Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When I run this program I get this error . I don't know how to solve . Help me finding it Please.

12-02 23:04:34.427: E/JSON Parser(1629): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray

Code:

public class Http
{

public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
private static HttpClient mHttpClient;
private static HttpClient getHttpClient() {

      if (mHttpClient == null) {
       mHttpClient = new DefaultHttpClient();

       final HttpParams params = mHttpClient.getParams();
       HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
       HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
       ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
      }

      return mHttpClient;
     }


public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
    try {


           HttpClient client = getHttpClient();

           HttpGet request = new HttpGet();

           request.setURI(new URI(url));

           HttpResponse response = client.execute(request);

        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(response.getEntity());



            // Parse the JSON String into a JSONArray object.
            return JSONArray(jsonString);

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

    }



public static JSONArray retrieveJSON(){
    {
          StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork()                  .penaltyLog().build());

        String getAllFreebiesURL="http://10.0.2.2/football365/cityList.php";
        JSONArray json = null;
        try {
            json = getJSONArrayFromUrl(getAllFreebiesURL);
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i("JSON",json+"A");
        //JSONArray json1 = new JSONArray(json);
        //json1.put(json);
        /*try {
            System.out.println(json1.get(2));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        return json;
    }
 }
 }
share|improve this question
 
did you get the json in log. –  Hariharan Dec 3 '13 at 4:22
 
looks like a character encoding problem –  user2310289 Dec 3 '13 at 4:23
 
@user2310289 show what kind of json response you are getting –  Aaღirkhan Dec 3 '13 at 4:23
 
@ Tamilan . yes that's the error that i got when i output json in log . –  user3032822 Dec 3 '13 at 4:24
 
@user3032822 post your logcat error. –  Hariharan Dec 3 '13 at 4:25
add comment

3 Answers

up vote 0 down vote accepted

Try out as below: You can parse your response as below:

    // Get our response as a String.
    String jsonString = EntityUtils.toString(response.getEntity());
   JSONObject m_jobj;
try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    for (int i = 0; i < m_ja.length(); i++) 
     {
                 JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }

Update your getJSONArrayFromurl() as below:

public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
    try {


           HttpClient client = getHttpClient();

           HttpGet request = new HttpGet();

           request.setURI(new URI(url));

           HttpResponse response = client.execute(request);

        try {
            // Get our response as a String.
            String jsonString = EntityUtils.toString(response.getEntity());
       JSONObject m_jobj;
    try {
    m_jobj = new JSONObject(jsonString);
    JSONArray m_ja = m_jobj.getJSONArray("cityData");
    /*for (int i = 0; i < m_ja.length(); i++) 
             {
             JSONObject m_obj = m_ja.getJSONObject(i);
                  String city=m_obj.getString("cityID");
                  String cityName=m_obj.getString("cityName");
                   //And so on get all the values.
               }*/


            // Parse the JSON String into a JSONArray object.
            return m_ja;

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

    }
share|improve this answer
 
I can't use loopin' . Tht's wht my leader said . What i get is only CityID . Could you help me . –  user3032822 Dec 3 '13 at 4:50
 
Without looping how will you get all the CityID ? –  GrIsHu Dec 3 '13 at 4:52
 
tht's wht I said her . But she command me to use something like dictionary Object or sth like that :( –  user3032822 Dec 3 '13 at 4:56
 
Its the only possible with the looping only if you want to get all the CityID from your response. Its a common sense man. –  GrIsHu Dec 3 '13 at 4:58
 
I'm really sorry . Thanks for your patient . –  user3032822 Dec 3 '13 at 5:00
show 2 more comments

change your method to :

public static JSONArray getJSONArrayFromUrl(String url) throws Exception {
try {


       HttpClient client = getHttpClient();

       HttpGet request = new HttpGet();

       request.setURI(new URI(url));

       HttpResponse response = client.execute(request);

    try {
        // Get our response as a String.
        String jsonString = EntityUtils.toString(response.getEntity());
        JSONObject jsonObject = new JSONObject(jsonString);
        String[] names = JSONObject.getNames(jsonObject);
        JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));


        // Parse the JSON String into a JSONArray object.
        return jsonArray;

    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return null;

}
share|improve this answer
add comment

You cant cast string to JsonArray, First cast it to a JsonObject

try {
       JSONObject jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

Then get the JsonArray from it

jObj.getJSONArray(NAME);

share|improve this answer
 
when i did this , JAva.lang.String cannot be convert to JSonObject. that's the error i got . –  user3032822 Dec 3 '13 at 4:36
 
updated code try that –  Viswanath L Dec 3 '13 at 4:42
 
Not working. Thanks for your patient Arju . :( –  user3032822 Dec 3 '13 at 4:50
 
can you post your updated code –  Viswanath L Dec 3 '13 at 4:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.