您的位置:首页 > 理论基础 > 计算机网络

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

2016-12-31 12:51 543 查看
  编写了如下代码,功能是获得网页的源代码信息:

  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);没有被执行。问题出在哪里,如何解决???
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐