Android HttpRequest

http 




代碼例子:

                               

 //生成一個請求對象
				HttpGet httpGet = new HttpGet("http://www.baidu.com");
				//生成一個Http客戶端對象
				HttpClient httpClient = new DefaultHttpClient();
				//使用http客戶端發送請求對象
				InputStream inputStream = null;
				try {
					//httpResponse就是代表響應對象
					httpResponse=httpClient.execute(httpGet);
					//httpEntity包含的就是返回的消息內容
					httpEntity = httpResponse.getEntity();
					inputStream =httpEntity.getContent();
					BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
					StringBuilder result=new StringBuilder("");
					String line = "";
					while((line=reader.readLine())!=null){
						result.append(line);
					}
					System.out.println(result.toString());
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				finally{
					try {
						inputStream.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}

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