【发布时间】:2015-09-03 13:22:21
【问题描述】:
我想从我的 prestashop 网络服务中获取一些 json 对象或子数组。我可以通过带有 AppKey 的 url 在浏览器中获取我的 json。但是这个请求在 android request.my son 对象中不起作用,因为 http 请求无法接收它。 我的问题是如何在android中通过请求获取这个儿子,浏览器可以成功获取。
这是我在浏览器中的 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://MyApp-Key@elsevan.com/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)
【问题讨论】:
-
不是真正的答案或其他东西,但您可以尝试使用 Retrofit 库从服务器获取 json
标签: android json web-services url prestashop-1.6