在Android的Activity中使用HttpURLConnection连接到服务端时抛出异常,Access denied。第一个想到是权限问题。然后就尝试将INTERNET权限加上:在Manifest中加上:

<uses-permission android:name="android.permission.INTERNET" />

还是抛异常!看异常信息,和StrictMode有关,想到和Android版本有关,然后在Activity中加上如下代码,问题解决:

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

完整代码:

package com.example.quhao_test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.TextView; public class MainActivity extends Activity { @TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) { System.out.println("xxxxxxxxxxxxxxx"); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); String strUrl = "http://146.11.24.100:9081/testcontroller/test1?arg=2222";
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
URL url = null;
try {
url = new URL(strUrl);
System.out.println(url.getPort());
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
BufferedReader br = new BufferedReader(in);
String result = "";
String readerLine = null;
while((readerLine=br.readLine())!=null){
result += readerLine;
}
in.close();
urlConn.disconnect(); System.out.println("r:"+result);
TextView textView = (TextView)this.findViewById(R.id.result);
textView.setText(result);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

原创文章,转载请指明出处:
withiter

解决Andriod使用HttpURLConnection 失败问题的更多相关文章

  1. RSA加密-解密以及解决超长内容加密失败解决

    加解密(没有使用到证书):https://blog.csdn.net/qy20115549/article/details/83105736 生成证书网站:https://blog.csdn.net/ ...

  2. Docker 安装 Jenkins , 并解决初始安装插件失败

    安装 Jenkins 后,初始化下载插件总是失败,导致安装不成功,重试好几次都是卡在安装插件那. 这里记录下 Docker 下怎么安装 Jenkins ,并解决初始安装插件失败问题. 安装插件失败,其 ...

  3. 【解决】安装compass失败(gem install compass)

    原始日期:2016-01-25 16:26 这个问题比较常见.   很多人在安装ruby后再使用gem install compass命令安装compass,发现安装失败.     [解决方法:] / ...

  4. 解决配置Windows Update失败问题

    大家都清楚电脑总是需要更新一些补丁,不过,很多系统用户发现更新了补丁之后,开机会出现windows update更新失败的情况,提示“配置Windows Update失败,还原更改,请勿关闭计算机”信 ...

  5. 解决linux用户切换失败 su:execute /usr/bin 没有权限

    问题描述: 回宿舍前,在root用户中安装fish,并修改其shell为fish.回宿舍之后,在图形界面用root用户进行登陆,莫名其妙登陆失败.没有任何提示信息,直接回到登陆界面.用非root用户登 ...

  6. 使用 EF Core 的 EnableRetryOnFailure 解决短暂的数据库连接失败问题

    阿里云服务器有时会出现短暂的连接不上数据库服务器(RDS)的问题,之前由于没有启用 Entity Framework Core 的失败重试功能(默认是禁用的),短暂的连接失败立马会引发下面的异常从而出 ...

  7. 解决配置Windows Update失败,还原更改问题

    问题描述 由于配置Windows Update失败,还原更改状态下无法正常关机.只能长按电源键关机后进入WinPE环境. 解决步骤 进入WinPE环境->选择Dism++->选择版本-&g ...

  8. 如何解决rar文件解压缩失败

    附件经常会是一系列的压缩文件,下载是默认文件名是一个随机数字.因而下载完会出现压缩文件解压缩失败解决方法:下载时重命名为带一定顺序的文件名,如文件1,文件2,文件3等 如何解决单个文件解压失败?论坛中 ...

  9. windows 环境下python 安装 pypcap 并用pyinstaller打包到exe,解决DLL 加载失败。

    安装 PYQT5 pypcap 环境: windows10_x64 python3.6.3 pycharm2017.2.4 备注: 需要安装 Visual C++ Build Tools 2015 可 ...

随机推荐

  1. 关于Linux系统清理/tmp/文件夹的原理

    转自:http://www.opsers.org/base/clean-up-on-the-linux-system-tmp-folder-you-may-want-to-know.html 我们知道 ...

  2. Oracle10g数据泵EXPDP和IMPDP备份与恢复数据

    Oracle10g数据泵EXPDP和IMPDP备份与恢复数据 一.数据库备份前准备工作 新建备份DIRECTORY目录,并授权给用户 步骤: 1.登录sqlplus 账户名:ptemp 密码:0000 ...

  3. Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change

    今天布置环境的时候发现一个问题:Your working copy is out of date. Try pulling from the remote to get the latest chan ...

  4. 关于运行robotium提示连接不上jar问题

    robotium运行测试helloworld报错: java.lang.NoClassDefFoundError: com.jayway.android.robotium.solo.Solo at c ...

  5. cocos2d-x的经历(含源码——)

    源于手游的火爆和大环境的影响,最近在学习cocos2d-x.其实以前一直有想搞andriod和ios的尝试,可惜本屌丝没钱买mac os x,只好先开始学习andriod,于是买了小米2s作为测试机, ...

  6. c语言,变长数组

    下面这个结构体,可以在malloc的时候指定数据data的长度,这样的形式就是变长数组:typedef struct{ int data_len; char data[0];//或char data[ ...

  7. 终于懂了:Delphi消息的Result完全是生造出来的,不是Windows消息自带的(Delphi对Windows编程体系的改造越大,学习收获就越大)——消息是否继续传递就看这个Result

    Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型, 而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据的含义也不一样. t ...

  8. sql: PL/SQL proc

    A PL/SQL block has the following structure: [DECLARE declaration_statements ] BEGIN executable_state ...

  9. DBA 应该要注意Linux 环境下的一些操作

    DBA 对OS的依赖.一丁点儿也不亚于DB.对于Oracle DBA.尤为突出     DB和OS的感情也与日俱增.耦合度高的让人一度以为这两要劳燕双飞了 例如.Oracle里面. 而且.故障诊断以及 ...

  10. AIX用户管理

    用户和组管理     /etc/passwd     /etc/security/.profile     /etc/security/limits     /etc/security/passwd ...