网络调用引发IO异常,android中的连接被拒绝在android设备中不工作

网络调用引发IO异常,android中的连接被拒绝在android设备中不工作,android,http,network-protocols,Android,Http,Network Protocols,我正在尝试从Instagramweb服务器获取值,我的代码在emulator中运行良好,但在android设备中不工作。它表示IOException和连接拒绝错误 我在清单文件中给出了和, 我使用的URL是用户id]/?access\u token=here access token 对于我正在使用的网络呼叫: URL url = new URL(urls); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnec

我正在尝试从
Instagram
web服务器获取值,我的代码在emulator中运行良好,但在android设备中不工作。它表示
IOException
和连接拒绝错误

我在
清单
文件中给出了
, 我使用的
URL
是用户id]/?access\u token=here access token

对于我正在使用的网络呼叫:

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
是一种方法:

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;
}
我的异步任务类

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);
        }
    }
}
公共类JSONParser扩展了异步任务{
私人语境;
私有错误;
私有请求侦听器回调;
私有进程对话;
私有字符串url;
公共JSONParser(上下文上下文、字符串url、RequestListener回调){
this.context=上下文;
this.callback=回调;
this.error=error.UNKNOWN;
this.url=url;
}
公共JSONParser showProgressDialog(int-messageId){
返回showProgressDialog(null,context.getString(messageId));
}
公共JSONParser showProgressDialog(字符串消息){
返回showProgressDialog(空,消息);
}
公共JSONParser showProgressDialog(int titleId,int messageId){
返回showProgressDialog(context.getString(titleId)、context.getString(messageId));
}
公共JSONParser showProgressDialog(字符串标题、字符串消息){
progressDialog=新建progressDialog(上下文);
如果(标题!=null){
progressDialog.setTitle(标题);
}
progressDialog.setMessage(消息);
progressDialog.setUndeterminate(true);
progressDialog.setCancelable(假);
归还这个;
}
@凌驾
受保护的void onPreExecute(){
如果(progressDialog!=null){
progressDialog.show();
}
}
@凌驾
受保护的JSONObject doInBackground(无效…参数){
JSONObject结果=null;
if(isNetworkReach()){
试一试{
字符串url=url;
URL=新URL(URL);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.connect();
字符串响应=streamToString(urlConnection.getInputStream());
结果=新的JSONObject(响应);
}捕获(IOE异常){
ALog.e(“IOException:%s”,例如getMessage());
error=error.IO\u error;
}捕获(解析异常){
ALog.e(“解析异常:%s”,例如getMessage());
error=error.PARSE_error;
}捕获(JSONException e){
ALog.e(“JSONException:%s”,e.getMessage());
error=error.PARSE_error;
}捕获(例外e){
ALog.e(“未知异常:%s”,e.getMessage());
error=error.UNKNOWN;
}
}否则{
error=error.NETWORK\u不可用;
}
返回结果;
}
私有字符串streamToString(InputStream is)引发IOException{
字符串str=“”;
如果(is!=null){
StringBuilder sb=新的StringBuilder();
弦线;
试一试{
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(is));
而((line=reader.readLine())!=null){
某人附加(行);
}
reader.close();
}最后{
is.close();
}
str=sb.toString();
}
返回str;
}
@凌驾
受保护的void onPostExecute(JSONObject JSONObject){
如果(progressDialog!=null){
progressDialog.disclose();
}
如果(!isCancelled()){
if(jsonObject!=null){
onRequestSuccess(jsonObject);
}否则{
callback.onRequestFailure(错误);
}
}
}

只有不在设备中工作,它才在emulator上工作。首先检查设备是否有活动的internet连接,如果为true,则进行http连接。 像这样:

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();
if (isConnected) {
  URL url = new URL(urls);
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setRequestMethod("GET");
  urlConnection.setDoInput(true);
  urlConnection.setDoOutput(true);
  urlConnection.connect();

// more stuff here
}

希望有帮助。

JSONParser。doInBackground@77:IOException:与我提供的链接相同here@John请发布完整的日志,不仅仅是itI的一部分。我认为您没有使用asynctask获取数据。这就是为什么它会给您一个例外。同时发布您的日志logcat@nikis我只得到了以上的行,我再次粘贴在这里JSONParser.doInBackground@77:IOException:user id]/?access_token=[此处为access token]@John尝试调用
e.printStackTrace()
查看完整信息