如何解决httpResponse = httpClient.execute(httpGet);无法执行的问题?

半岛铁盒里的猫 2014-12-05 12:25:12
编写了如下代码,功能是获得网页的源代码信息:
MainActivity.java:
package com.example.http01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private Button button = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new ButtonListener());
}

class ButtonListener implements OnClickListener{

private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private InputStream inputStream = null;
@Override
public void onClick(View arg0) {
new Thread(){

@Override
public void run() {
Log.d("yinan", "run!"); ////////
HttpGet httpGet = new HttpGet("http://www.baidu.com");
HttpClient httpClient = new DefaultHttpClient();
try {
Log.d("yinan", "httpClient");//////////
httpResponse = httpClient.execute(httpGet);
Log.d("yinan", "httpResponse");//////////
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String str = "";
String line = "";
while((line = reader.readLine()) != null){
str = str +"\n"+line;
}
Log.d("yinan", str);
} catch (ClientProtocolException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}.start();

}

}


}


main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送http请求"/>


</RelativeLayout>


Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.http01"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.http01.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


运行结果是:yinan(12695): run!
yinan(12695): httpClient
说明httpResponse = httpClient.execute(httpGet);没有被执行。问题出在哪里,如何解决???
...全文
1027 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 birdsaction 的回复:
如果没有异常打印,可能是没有设置超时,你设置一个超时 看看是不是无法连接网络。
具体如何设置,我是新手。。
  • 打赏
  • 举报
回复
引用 1 楼 birdsaction 的回复:
如果没有异常打印,可能是没有设置超时,你设置一个超时 看看是不是无法连接网络。
具体如何设置,我是新手。。
  • 打赏
  • 举报
回复
引用 1 楼 birdsaction 的回复:
如果没有异常打印,可能是没有设置超时,你设置一个超时 看看是不是无法连接网络。
具体如何设置,我是新手。。
Birds2018 2014-12-05
  • 打赏
  • 举报
回复
如果没有异常打印,可能是没有设置超时,你设置一个超时 看看是不是无法连接网络。
Birds2018 2014-12-05
  • 打赏
  • 举报
回复
如果没有异常打印,可能是没有设置超时,你设置一个超时 看看是不是无法连接网络。
Birds2018 2014-12-05
  • 打赏
  • 举报
回复
如果没有异常打印,可能是没有设置超时,你设置一个超时 看看是不是无法连接网络。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧