I am trying to get a value from a web server Instagramin my code that works fine in an emulator but doesn't work in an Android device. He says IOExceptionrefusal to refuse connection.
I have given <uses-permission android:name="android.permission.INTERNET"/>and <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>in the Manifestfile that is used URLI https://api.instagram.com/v1/users/[ User ID] /? access_token = here is the access token
For a network call, I use:
URL url = new URL(urls);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
String response = streamToString(urlConnection.getInputStream());
result = new JSONObject(response);`
streamToString - method:
private String streamToString(InputStream is) throws IOException {
String str = "";
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} finally {
is.close();
}
str = sb.toString();
}
return str;
}
my AsyncTask class
public class JSONParser extends AsyncTask<Void, Void, JSONObject> {
private Context context;
private Error error;
private RequestListener callback;
private ProgressDialog progressDialog;
private String url;
public JSONParser(Context context, String url, RequestListener callback) {
this.context = context;
this.callback = callback;
this.error = Error.UNKNOWN;
this.url = url;
}
public JSONParser showProgressDialog(int messageId) {
return showProgressDialog(null, context.getString(messageId));
}
public JSONParser showProgressDialog(String message) {
return showProgressDialog(null, message);
}
public JSONParser showProgressDialog(int titleId, int messageId) {
return showProgressDialog(context.getString(titleId), context.getString(messageId));
}
public JSONParser showProgressDialog(String title, String message) {
progressDialog = new ProgressDialog(context);
if (title != null) {
progressDialog.setTitle(title);
}
progressDialog.setMessage(message);
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
return this;
}
@Override
protected void onPreExecute() {
if (progressDialog != null) {
progressDialog.show();
}
}
@Override
protected JSONObject doInBackground(Void... params) {
JSONObject result = null;
if (isNetworkReachable()) {
try {
String urls = url;
URL url = new URL(urls);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
String response = streamToString(urlConnection.getInputStream());
result = new JSONObject(response);
} catch (IOException e) {
ALog.e("IOException : %s", e.getMessage());
error = Error.IO_ERROR;
} catch (ParseException e) {
ALog.e("ParseException : %s", e.getMessage());
error = Error.PARSE_ERROR;
} catch (JSONException e) {
ALog.e("JSONException : %s", e.getMessage());
error = Error.PARSE_ERROR;
} catch (Exception e) {
ALog.e("UnknownException : %s", e.getMessage());
error = Error.UNKNOWN;
}
} else {
error = Error.NETWORK_UNAVAILABLE;
}
return result;
}
private String streamToString(InputStream is) throws IOException {
String str = "";
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} finally {
is.close();
}
str = sb.toString();
}
return str;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if (progressDialog != null) {
progressDialog.dismiss();
}
if (!isCancelled()) {
if (jsonObject != null) {
callback.onRequestSuccess(jsonObject);
} else {
callback.onRequestFailure(error);
}
}
}
Just does not work in the device, it works on the emulator
source
share