android发送请求失败

pzzc18 pzzc18 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是一个执行访问外网接口返回数据的一个方法

private String getPDAServerData(String url){
HttpClient client = new DefaultHttpClient();
HttpPost request;
String out="";
try {
request = new HttpPost(new URI(url));
HttpResponse response = client.execute(request);
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
out = EntityUtils.toString(entity);
//new AlertDialog.Builder(this).setMessage(out).create().show();
}
}

} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return out;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)this.findViewById(R.id.msg);
// id,title,thumb,description,inputtime
String str = getPDAServerData(url);
String []arr = str.split("&");//这里是自定义的字符解析,可以忽略
String html = "";
for(int i=0;i<arr.length;i++){
String []arr2 = arr[i].split("|");
html += "<img src=\""+arr[2]+"\" width=\"80\" height=\"80\"/>"+arr2[1];
if(i%2!=0)
html+="<br>";
}
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);

}

运行虚拟机以后,提示 HttpResponse response = client.execute(request); 报错了
AndroidManifest.xml中增加了下面这句话,还是一样。不知道还需要做什么才不报错?
<uses-permission android:name="android.permission.INTERNET" />
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
pzzc18
Android 80,490 成员 91,371 内容