0

I want to get some json object or son array from my prestashop web service. I can get my json in browser by url with AppKey. but this request not worked in android request.my son object is null because http request can't receive it. my question is how to get this son by request in android , browsers can get it successfully.

this is my json response in browser:

{"products":[{"id":107},{"id":120},{"id":767},{"id":26},{"id":27}]}

but I can't get this json in android, or maybe my request is wrong:

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

    }
}
}

my log:

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)
Tohid Najafi
  • 31
  • 1
  • 6
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Sep 03 '15 at 13:39
  • Not really an answer or something, but you can maybe try the Retrofit library for get json from server – R. Adang Sep 03 '15 at 14:49

3 Answers3

1

You are never getting a json array, your "json" should be an Array. Ones you get your json response ("products" is the Json Array) you must do something like this:

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

Also, did you check the actual json? Are you getting that exactly response? The null pointer over doInBackground it's because you are calling a jsonArray totally null.

Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
  • my problem is getting son by request – Tohid Najafi Sep 03 '15 at 15:30
  • did you try using http://loopj.com/android-async-http/ ? it's a really good async with all the GET, POST, PUT, DELETE request in a simple steps. – Mariano Zorrilla Sep 03 '15 at 15:40
  • I used it , very good. but it runs onFailure methode . because it can't do http request correctly. my request error response is :B@53790420 – Tohid Najafi Sep 03 '15 at 16:12
  • loopj response is using bytes... you need to "parse" the response from Bytes to Strings. Like this: String str = new String(bytes, "UTF-8"); and check if API is working correctly. – Mariano Zorrilla Sep 03 '15 at 18:14
1

I solved my problem . it was http client authorize:

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());
            }
}
}
Tohid Najafi
  • 31
  • 1
  • 6
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);
}
fweigl
  • 21,278
  • 20
  • 114
  • 205
  • nope! it doesn't work! it requires username again. in browsers, this link requires user pass! My answer worked successfully. – Tohid Najafi Sep 10 '15 at 15:07