从Web服务URL获取json数组

时间:2015-09-03 13:22:21

标签: android json web-services url prestashop-1.6

我想从prestashop web服务中获取一些json对象或子数组。我可以通过url和AppKey在浏览器中获取我的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)

3 个答案:

答案 0 :(得分:1)

你永远不会得到一个json数组,你的&#34; json&#34;应该是一个数组。你得到你的json响应(&#34;产品&#34;是Json数组),你必须做这样的事情:

    JSONObject json = jParser.makeHttpRequest("http://MyApp-Key@elsevan.com/api/products", "GET", params);

    JSONArray json_products = json.getJSONArray("products");

    for(int i = 0; i < json_products.length(); i++ {

        JSONObject c = json_products.getJSONObject(i);

        Log.e("Object ID:", c.getString("id");
    }

另外,你检查了实际的json吗?你得到了那个确切的回应吗?它指向doInBackground的空指针,因为你正在调用jsonArray完全为空。

答案 1 :(得分:1)

我解决了我的问题。它是http客户端授权:

client.setBasicAuth("MyApp-Key", "");
        client.get("http://elsevan.com/api/products/107", new JsonHttpResponseHandler(){

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Toast.makeText(getActivity(), "Success!", Toast.LENGTH_LONG).show();


                Log.d("android OK", response.toString());
            }
}
}

答案 2 :(得分:0)

JSONObject json = jParser.makeHttpRequest("http://MyApp-Key@elsevan.com/api/products", "GET", params);

JSONArray array = json.get("products"); 

for(int i = 0; i < jsonArray.length(); i++ {
     JSONObject c = jsonArrays.getJSONObject(i);
}