0

http からの応答をフォーマットするのを手伝ってください。AsynckTask での投稿

protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
         HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http/test.php");

            try {
                // Add user name and password
             String latitude = "44.754535";
             String longitude = "11.178589";
             String range = "222";



               List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair(2);
                nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
                nameValuePairs.add(new BasicNameValuePair("longitude",longitude));
                nameValuePairs.add(new BasicNameValuePair("range", range));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                Log.w("SENCIDE", "Execute HTTP Post Request");
                HttpResponse response = httpclient.execute(httppost);

                String str = inputStreamToString(response.getEntity().getContent()).toString();
                Log.w("SENCIDE", str);

                if(str.toString().contains("da"))
                {

                content=str.toString();

                }else
                {

                 System.out.print(str);             
                }

            } catch (ClientProtocolException e) {
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
            }
            return content;
        } 

ログはこれです:

`{"success":0,"error_message":" 16.07 km da Motta"}

`

したがって、目標は、php 部分を変更せずに16.07 km da Mottaだけにすることです。配列に 3 つの項目があることを確認してください。配列から 3 番目のオブジェクトを取得するためのいくつかのアイデアがあります。事前にTnx。

4

1 に答える 1

1

サーバーからの応答でJSONObjectを取得しています。あなたはそれから値を得ることができます:

// convert Response String to JSONObject
JSONObject json=new JSONObject(str); //str=response string from server

// set error_message from json
String str_error_message=json.getString("error_message");

// set success from json
String str_success=json.getString("success");
于 2013-01-30T16:59:17.603 に答える