0

please help me to format my response from http. Posting with 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;
        } 

The log is this one :

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

`

So the goal is to have only 16.07 km da Motta without changing php part. See there are 3 items in aray, have some ideas to get 3rd object from array. Tnx in advance.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
user2021505
  • 242
  • 1
  • 12

1 Answers1

1

you are getting JSONObject in response from server . you can get values from it as :

// 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");
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213