如何解决httpResponse = httpClient.execute(httpGet);无法执行的问题?
来源:互联网 发布:淘宝返利管理推荐关闭 编辑:程序博客网 时间:2024/06/09 17:36
编写了如下代码,功能是获得网页的源代码信息:
MainActivity.java:
main.xml:
Manifest:
运行结果是:yinan(12695): run!
yinan(12695): httpClient
说明httpResponse = httpClient.execute(httpGet);没有被执行。问题出在哪里,如何解决???
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);没有被执行。问题出在哪里,如何解决???
0 0
- 如何解决httpResponse = httpClient.execute(httpGet);无法执行的问题?
- httpClient访问网络,httpclient.execute(httpGet)方法不执行问题
- 在AsyncTask的doInBackground方法中调用HttpClient的execute方法,HttpGet地址为内网时无问题,部署到外网就不执行了
- Android 无法用HttpClient.execute(httpGet)访问网页
- httpclient HttpResponse response = client.execute(post);报空指针问题
- Android 中//此句无响应 HttpResponse httpResponse = httpClient.execute(post);
- response = httpClient.execute(httpGet)不返回response
- HttpClient get请求在HttpResponse中无法获得Location的问题
- HttpClient get请求在HttpResponse中无法获得Location的问题
- HttpClient get请求在HttpResponse中无法获得Location的问题
- HttpResponse response = client.execute(httpPost);在4.0系统中运行异常的问题
- HttpClient包里面不包含HttpResponse的问题
- HttpClient和HttpResponse的使用
- 如何解决HttpClient类在Android新版本中无法添加的问题
- HttpClient的execute的阻塞问题
- HttpClient--HttpGet的使用样式
- HttpClient--HttpGet的使用样式
- HttpClient.execute() 阻塞问题
- 知乎爬虫之5:爬虫优化
- android 不规则宫格布局如何实现?
- 14模板方法模式
- codeforces Good Bye 2016-B New Year and North Pole
- android 按menu键菜单不会弹出来 没反应
- 如何解决httpResponse = httpClient.execute(httpGet);无法执行的问题?
- https 客户端和服务器的证书必须是同一个
- jsp 显示mysql数据库信息
- ios 使用音频队列播放amr格式转换成wav的格式,只有嘈杂声
- SDK更新问题?求助!!!
- 15策略模式
- yii2数据库操作汇总
- SimpleAdapter重写时响应CheckBox复选框状态改变-源码修改
- 代码染色!