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();
			}
		});
	}


發佈了41 篇原創文章 · 獲贊 14 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章