Yogesh Kumar
Yogesh Kumar

Reputation: 712

trying to get json object from php to android

i am trying to parse an array from my php script to my android app. my android code

public void func4(View view)throws Exception
{final String TAG_CONTACTS = "response";
    AsyncHttpClient client = new AsyncHttpClient();
    RequestParams rp = new RequestParams();
    rp.put("pLat", "SELECT officer_name FROM iwmp_officer");

    client.post("http://10.0.2.2/conc3.php", rp, new AsyncHttpResponseHandler() {
        public final void onSuccess(String response) {
            // handle your response here
            ArrayList<String> User_List = new ArrayList<String>();


            try
        {
      JSONArray jArray = new JSONArray(response.toString());
       // JSONObject jsonObject = new JSONObject(response);
      for (int i = 0; i < jArray.length(); i++)
      {
          JSONObject json_data = jArray.getJSONObject(i);



         User_List.add(json_data.getString(TAG_CONTACTS));
         String s = User_List.get(0).toString();
         tx.setText(s);
      }

         } 
            catch (Exception e)
        {
                tx.setText((CharSequence) e);
         }

        }

        @Override
        public void onFailure(Throwable e, String response) {
            // something went wrong
            tx.setText(response);
        }               
    });
}

now i am sending or ECHOing a jSON array from my php code, which is being read into the response object of string type.

<?php $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); echo json_encode($cars); exit; ?>

now error i am getting is org.json.JSONException: Value Volvo at 0 of type java.lang.String cannot be converted to JSONObject

i think thar onSuccess func accept string parameter and i m sending json as a parameter to it.. thats what causing problem please help.

Upvotes: 0

Views: 1826

Answers (2)

Yogesh Kumar
Yogesh Kumar

Reputation: 712

Answer found, i guess i was trying to convert an already String converted object from json into string again. well to be precise i ust deleted JSONObject json_data = jArray.getJSONObject(i); and it worked for me. Now i am able to save all values in my array.

Upvotes: 0

Make it Simple
Make it Simple

Reputation: 1882

Try Like this

    JSONObject json = jsonParser.makeHttpRequest(url_get_contact, "GET", params);
         Log.d("All Groups: ", json.toString());
         try {
             int success = json.getInt(TAG_SUCCESS);
             if (success == 1) {
                 groups = json.getJSONArray(TAG_GROUP);
                 System.out.println("Result Success+++"+groups);
                 for (int i = 0; i < groups.length();i++) {
                 JSONObject c = groups.getJSONObject(i);
                 String name = c.getString(TAG_CONTACTS);
                 System.out.println("Checking ::"+name);
             namelist.add(name); // namelist is ur arraylist
                 }
             }else {            
                 showAlert();
             }
         } catch (JSONException e) {
             e.printStackTrace();
         }
         return null;

Let me know ur problem solved or not...

Upvotes: 1

Related Questions