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)