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 公里 da Motta 。看到数组中有 3 个项目,有一些想法可以从数组中获取第 3 个对象。提前Tnx。

4

1 回答 1

1

您从 server 获得 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 回答