我想從我的prestashop Web服務中獲得一些json對象或兒子數組。我可以使用AppKey通過url在瀏覽器中獲取我的json。但是這個請求在android request.my子對象中不起作用,因爲http請求無法接收它。 我的問題是如何在Android中通過請求獲取這個兒子,瀏覽器可以成功獲取它。從Web服務url獲取json數組
這是瀏覽器的json響應:
{"products":[{"id":107},{"id":120},{"id":767},{"id":26},{"id":27}]}
,但我不能在Android中得到這個JSON,或者也許我的要求是錯誤的:
public class Asyncs {
JSONParser jParser = new JSONParser();
JSONArray jsonArrays = new JSONArray();
public class GetProduct extends AsyncTask<String, String, ArrayList<String>> {
ArrayList<String> test = new ArrayList<>();
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected ArrayList<String> doInBackground(
String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest("http://[email protected]/api/products", "GET", params);
Log.d(" Products: >>>>>>>>>> ", json.toString());
try {
for (int i = 0; i < jsonArrays.length(); i++) {
JSONObject c = jsonArrays.getJSONObject(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return test;
}
@Override
protected void onPostExecute(final ArrayList<String> result) {
}
}
}
我的日誌:
2540-2553/? E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
09-03 12:43:20.444 2540-2553/? W/dalvikvm﹕ threadid=10: thread exiting with uncaught exception (group=0xa6270288)
09-03 12:43:20.444 2540-2553/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at Requests.Asyncs$GetProduct.doInBackground(Asyncs.java:61)
at Requests.Asyncs$GetProduct.doInBackground(Asyncs.java:31)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-isa-a- – njzk2
不是一個真正的答案或者什麼,但你也許可以嘗試改裝庫來獲取服務器的JSON –