-1

I make "GET" request in order to get a JSON object. When i try to read the response, an error is thrown. The error is descriped in the header. It's a really miracle for me, because when i run the app on the emulator or in the browser, i get JSON response, everything is ok. But when i run it on real phone, i see this error.... Will be grateful for any answer. Here is my code.

class Task extends AsyncTask<String, Void, String> {
StringBuilder builder;

@Override
protected String doInBackground(String... params) {
    HttpURLConnection connection;
    try {
        URL url = new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("log", "background error " + e);
    }
    return builder.toString();
}

@Override
protected void onPostExecute(String s) {

    Log.e("log", "response " + s);

    try {
        JSONObject jsonObject = new JSONObject(s);
        Log.e("log", "JSON = " + jsonObject);
    } catch (JSONException e) {
        e.printStackTrace();
        Log.e("log", "post error " + e);
    }

}

}

This is the String i get in post Execute

<html>                            <body>                                
<script type='text/javascript' charset='utf-8'>                                    
window.location.href = '/';                                </script>                            
</body>                        </html>                        

And here is the error i get

05-04 08:56:19.989 2731-2731/? W/System.err: org.json.JSONException: Value 
<html> of type java.lang.String cannot be converted to JSONObject
05-04 08:56:19.989 2731-2731/? W/System.err:     at 
org.json.JSON.typeMismatch(JSON.java:111)
05-04 08:56:19.989 2731-2731/? W/System.err:     at org.json.JSONObject.
<init>(JSONObject.java:158)
05-04 08:56:19.989 2731-2731/? W/System.err:     at org.json.JSONObject.
<init>(JSONObject.java:171)
05-04 08:56:19.989 2731-2731/? W/System.err:     at 
com.example.dshahzadyan.gettest.Task.onPostExecute(MainActivity.java:64)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
com.example.dshahzadyan.gettest.Task.onPostExecute(MainActivity.java:33)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
android.os.AsyncTask.finish(AsyncTask.java:602)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
android.os.AsyncTask.access$600(AsyncTask.java:156)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
android.os.Handler.dispatchMessage(Handler.java:99)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
android.os.Looper.loop(Looper.java:137)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
android.app.ActivityThread.main(ActivityThread.java:4517)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
java.lang.reflect.Method.invokeNative(Native Method)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
java.lang.reflect.Method.invoke(Method.java:511)
05-04 08:56:19.999 2731-2731/? W/System.err:     at 
David
  • 351
  • 3
  • 11

1 Answers1

0

The response you are getting is of type html and not json. So, onPostExecute method when the response string which is of html format is not able to convert to json. Make sure the url you are trying to hit is sending the json and not html

Bijesh
  • 317
  • 3
  • 11
  • thank you for answer, but the problem is that the response is of JSON format. I get response in JSON format when i run programm on emulator but not on real device. – David May 04 '17 at 06:19
  • I would agree with you if i hadn't try to make request using other URL. The effect is the same – David May 04 '17 at 06:20