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