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)