2014-10-03 2 views
0

문자열 값을 허용하는 JSONArray를 반환하지만 응용 프로그램을 실행할 때마다 아래 오류 메시지가 표시됩니다. 내 (GetCityDetails) 메서드가 null 값을 반환하고 있습니다. 내 구문과 logcat 정보는 아래와 같습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 감사합니다JSONObject를 JSONArray로 바꿀 수 없습니다 - 외부 데이터베이스

org.json.JSONException : $ 1 JSONArray로 변환 할 수 없습니다 org.json.JSONObject 유형의 null 값

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; 
} 


} 

로그 캣

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) 
,

답변

0

documentation은 json 형식의 콜렉션 또는 문자열을 전달하여 JSONArray를 인스턴스화 할 수 있지만 로그의 첫 번째 행에 나타나는 것처럼 null 값을 전달한다고 설명합니다.

디버그 convertInputStreamIntoString을 디버그하고 요청에 대한 잘못된 응답을 받았는지 확인하거나 다른 문자열을 얻지 못하는지 확인하십시오.

관련 문제