我正在運行asyncTask
以從HttpPost
中檢索JSON數組對象。從GET請求接收空的JSON數組
從WebService的輸出是:
[
{
"id": 1,
"question": "What town were you born in?"
},
{
"id": 2,
"question": "What is your mothers maiden name?"
},
{
"id": 3,
"question": "Who was your childhood hero?"
}
]
但得到JSONException
:
org.json.JSONException: End of input at character 0 of
其中提到JArray
在下面的代碼:
JSONArray JArray;
Context ra;
public SecurityQuestionsTask(Context _registerActivity) {
ra = _registerActivity;
JArray = new JSONArray();
}
@Override
protected Long doInBackground(String... langs) {
Long result = 0L;
String lang = Locale.getDefault().getLanguage();
String[] langArray = {"en", "de", "fr"};
if (!Arrays.asList(langArray).contains(lang)) {
lang = "en";
}
try {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://webservice.com/api/v1/securityquestions?lang=" + lang);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
JSONString = EntityUtils.toString(response.getEntity(), "UTF-8");
JArray = new JSONArray(JSONString);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
publishProgress("Retrieving security questions...");
return result;
}
我是解析response
至JSONArray
相關ctly?
編輯:
這是Web服務的響應:
return Response.status(200).entity(json.toString()).build();
是EntityUtils.toString(response.getEntity(), 「UTF-8」);返回預期的JSON字符串?它被分配給一個傳遞給JSONArray構造函數的變量嗎? – nomis 2015-03-02 12:18:34
分享同樣的反應,得到'JSONString',因爲異常說試圖將無效字符串轉換爲JSONArray或JSONObject – 2015-03-02 12:20:11
您是否嘗試輸入預期的數據到'JSONString'變量如'JSONString =「[{\」id \「:1 ,「問題」:「你出生在哪個城鎮?」)「'?它需要診斷您的錯誤是來自http通信還是內容解析。 – Youngjae 2015-03-02 12:26:53