SDK 4.0 使用httpClient访问网络资源,报异常

package mars.http;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HttpActivity extends Activity {

	private Button requestButton = null;
	HttpResponse httpResponse = null;
	HttpEntity httpEntity = null;
	InputStream inputStream = null;
	BufferedReader bufferedReader = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		requestButton = (Button) findViewById(R.id.requestButton);
		requestButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = "http://www.baidu.com";
				HttpGet httpGet = new HttpGet(url);
				HttpClient httpClient = new DefaultHttpClient();
				try {
					httpResponse = httpClient.execute(httpGet);
					httpEntity = httpResponse.getEntity();
					inputStream = httpEntity.getContent();
					bufferedReader = new BufferedReader(new InputStreamReader(
							inputStream));
					StringBuffer result = new StringBuffer();
					String temp = "";
					while ((temp = bufferedReader.readLine()) != null) {
						result.append(temp);
					}
					System.out.println(result.toString());
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					try {
						inputStream.close();
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		});
	}
}

 如果ActivityManifest中的minSDK

<uses-sdk android:minSdkVersion="14" />

 配置如果写成 14 就会报异常。改为14一下就正常。不知道是不是bug

 

你可能感兴趣的:(httpclient,Inputstream,空指针,访问网络,sdk4.0)