0

I'm returning a JSONArray that accepts a String value but I'm getting the below error message whenever I run my application. My (GetCityDetails) method is returning a null value. My syntax and logcat information is below. What am I doing wrong? Thanks

org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray

JSON Array thats being returned online [{"city":"Hialeah"},{"city":"Daytona Beach"},{"city":"Hollywood"},{"city":"Marianna"}]

@SuppressLint("NewApi")
public JSONArray GetCityDetails(String StateID) {

    @SuppressWarnings("deprecation")
    String url = "http://mywebsite.com/getCity.php?StateID="+URLEncoder.encode(StateID);

    HttpEntity httpEntity = null;

    try{

         DefaultHttpClient httpClient = new DefaultHttpClient();
         HttpGet httpGet = new HttpGet(url);

         HttpResponse httpResponse = httpClient.execute(httpGet);

         httpEntity = httpResponse.getEntity();


    } catch(ClientProtocolException e){
        e.printStackTrace();

    } catch(IOException e){
        e.printStackTrace();
    }


    JSONArray jsonArray = new JSONArray();
    if(httpEntity !=null){
        try{

            InputStream entityResponse = httpEntity.getContent();
            String entityResponseAfterFunctionCall = convertInputStreamIntoString(entityResponse);

            Log.e("Entity Response From GetCityDetails Class: ", entityResponseAfterFunctionCall);

              jsonArray = new JSONArray(entityResponseAfterFunctionCall);
        } catch(JSONException e){
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    return jsonArray;
}

public String convertInputStreamIntoString(InputStream entityResponse) throws IOException{

    StringWriter writer = new StringWriter();
    IOUtils.copy(entityResponse, writer);
    String theString = writer.toString();
    return theString;
}


}

Logcat

01-01 19:11:36.719: E/Entity Response From GetCityDetails Class:(3938): null
01-01 19:11:36.729: W/System.err(3938): org.json.JSONException: Value null of type   org.json.JSONObject$1 cannot be converted to JSONArray
01-01 19:11:36.729: W/System.err(3938):     at org.json.JSON.typeMismatch(JSON.java:107)
01-01 19:11:36.729: W/System.err(3938):     at org.json.JSONArray.<init>(JSONArray.java:91)
01-01 19:11:36.729: W/System.err(3938):     at org.json.JSONArray.<init>(JSONArray.java:103)
01-01 19:11:36.729: W/System.err(3938):     at com.example.bhphprices.com.APIConnector.GetCityDetails(APIConnector.java:120)
01-01 19:11:36.729: W/System.err(3938):     at com.example.bhphprices.com.CityDetailsActivity$GetAllStates.doInBackground(CityDetailsActivity.java:64)
01-01 19:11:36.729: W/System.err(3938):     at  com.example.bhphprices.com.CityDetailsActivity$GetAllStates.doInBackground(CityDetailsActivity.java:1 )
01-01 19:11:36.739: W/System.err(3938):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-01 19:11:36.739: W/System.err(3938):     at    java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
01-01 19:11:36.739: W/System.err(3938):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
01-01 19:11:36.739: W/System.err(3938):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
01-01 19:11:36.739: W/System.err(3938):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
01-01 19:11:36.739: W/System.err(3938):     at java.lang.Thread.run(Thread.java:1019)

1 Answer 1

0

The documentation states that a JSONArray can be instantiated by passing a collection or a string with json format, but you are passing a null value, as appears in the first line of your log.

I would advice you to debug convertInputStreamIntoString and check whether you are receiving a bad response for your request, or something else is going off obtaining the string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.