0
我返回一个接受字符串值的JSONArray,但每次运行我的应用程序时都会收到下面的错误消息。我的(GetCityDetails)方法返回一个空值。下面是我的语法和logcat信息。我究竟做错了什么?由于JSONObject无法转换为JSONArray - 外部数据库
org.json.JSONException:类型的值null org.json.JSONObject $ 1不能被转换成JSONArray
在网上被退回JSON数组,多数民众赞成 [{ “城市”: “海厄利亚”} { “城市”: “代托纳比奇”},{ “城市”: “好莱坞”},{ “城市”: “玛丽安娜”}]
@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)