如何解决httpResponse = httpClient.execute(httpGet);无法执行的问题?
2016-12-31 12:51
543 查看
编写了如下代码,功能是获得网页的源代码信息:
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);没有被执行。问题出在哪里,如何解决???
相关文章推荐
- HttpClient get请求在HttpResponse中无法获得Location的问题
- httpClient访问网络,httpclient.execute(httpGet)方法不执行问题
- HttpClient get请求在HttpResponse中无法获得Location的问题
- Android项目中sdk 23无法使用HttpGet 和HttpClient的问题解决--只需改一行
- 在AsyncTask的doInBackground方法中调用HttpClient的execute方法,HttpGet地址为内网时无问题,部署到外网就不执行了
- HttpClient get请求在HttpResponse中无法获得Location的问题
- HttpClient的HttpGet请求乱码问题解决
- httpclient.execute()不执行也不报错解决办法
- 如何解决XmlHttp.responseText的乱码问题
- 如何解决SQL Server 2005无法重装问题 MicrosoftSQLNativeClient Sqlncli.msi
- httpclient HttpResponse response = client.execute(post);报空指针问题
- HttpClient Get请求中返回中文乱码问题解决
- 关于执行命令:sudo apt-get update后的“E: 无法解析或打开软件包的列表或是状态文件”问题解决
- android google map 开发中的常见问题及解决(无法获取 google map apiKey 和Couldn't get connection factory client)
- 工厂参观记:.NET Core 中 HttpClientFactory 如何解决 HttpClient 臭名昭著的问题
- HttpWebResponse无法准确获取CharacterSet导致网页数据乱码问题的解决
- ionic 到真相后$http.get()无法请求,导致空白的情况,如何解决
- HttpResponse response = client.execute(httpPost);在4.0系统中运行异常的问题
- Xutils-http-HttpClient get请求在HttpResponse中无法获得Location的问题
- response = httpClient.execute(httpGet)不返回response