Android之Http请求基础

一、使用HttpGet方式进行请求


private Button myButton = null;

	private HttpResponse httpResponse = null;
	private HttpEntity httpEntity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

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

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						//生成一个请求对象
						//参数:请求的地址
						HttpGet httpGet = new HttpGet("http://172.16.3.102:8080/Ibeacon/NewFile.jsp");
						//生成一个http客户端对象
						HttpClient httpClient = new DefaultHttpClient();
						
						InputStream inputStream = null;
						try {
							try {
								//获取请求的响应
								httpResponse = httpClient.execute(httpGet);
								//从响应当中获取数据流
								httpEntity = httpResponse.getEntity();
								inputStream = httpEntity.getContent();
								
								BufferedReader bufferedreader = new BufferedReader(
										new InputStreamReader(inputStream));
								//保存获取到的数据
								String result = "";
								String line = "";
								while ((line = bufferedreader.readLine()) != null) {
									result += line;
								}
								System.out.println(result);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}).start();
			}
		});
	}

二、使用HttpPost方式进行请求

private Button myButton = null;
	//声明请求的链接
	private String myURL = "http://172.16.3.102:8080/Ibeacon/Receive";
	HttpResponse httpResponse = null;
	HttpEntity httpEntity = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		myButton = (Button) findViewById(R.id.myButton);
		myButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						//键值对
						//用来设置请求的参数
						NameValuePair nameValuePair1 = new BasicNameValuePair(
								"uuid", "300001");
						NameValuePair nameValuePair2 = new BasicNameValuePair(
								"classify", "200001");
						List<NameValuePair> lists = new ArrayList<NameValuePair>();
						lists.add(nameValuePair1);
						lists.add(nameValuePair2);

						try {
							//HttpEntity既可以是请求体也可以是响应体
							//设置请求的参数
							HttpEntity requesthttpEntity = new UrlEncodedFormEntity(
									lists);
							//生成一个请求Post请求对象
							HttpPost httppost = new HttpPost(myURL);
							//设置请求体
							httppost.setEntity(requesthttpEntity);
							//生成一个http客户端对象
							HttpClient httpclient = new DefaultHttpClient();
							
							InputStream inputStream = null;
							BufferedReader reader = null;
							try {
								//获取请求响应
								httpResponse = httpclient.execute(httppost);
								//获取数据
								httpEntity = httpResponse.getEntity();
								inputStream = httpEntity.getContent();
								reader = new BufferedReader(
										new InputStreamReader(inputStream));
								//保存数据
								String result = "";
								String line = "";
								while ((line = reader.readLine()) != null) {
									result += line;
								}
								
								
								// System.out.println(result);

							} catch (Exception e) {
								// TODO: handle exception
								e.printStackTrace();
							} finally {
								try {
									inputStream.close();
									reader.close();
								} catch (Exception e2) {
									// TODO: handle exception
								}
							}
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}).start();
			}
		});
	}


你可能感兴趣的:(android,http请求)