Android進階2之Http操作訪問網路

來源:互聯網
上載者:User

操作步驟:

<1>

產生請求對象

HttpGet httpGet = new HttpGet("請求地址。。。。。");

<2>

產生用戶端對象

HttpClient httpClient = new DefaultHttpClient();

<3>

執行請求

HttpResponse httpResponse = httpClient.execute(httpGet);

<4>

接受響應

HttpEntity  httpEntity = httpResponse.getEntity();

<5>得到資料流

InputStream  inputStream = httpEntity.getContent();

注意:

要添加許可權: <uses-permission android:name="android.permission.INTERNET" />

具體實現:

package xiaosi.httpResponse;</p><p>import java.io.BufferedReader;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.InputStreamReader;</p><p>import org.apache.http.HttpEntity;<br />import org.apache.http.HttpResponse;<br />import org.apache.http.client.ClientProtocolException;<br />import org.apache.http.client.HttpClient;<br />import org.apache.http.client.methods.HttpGet;<br />import org.apache.http.impl.client.DefaultHttpClient;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.view.View;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.TextView;</p><p>public class HttpResponseActivity extends Activity<br />{<br />private Buttonbutton= null;<br />private TextViewtext= null;<br />private HttpResponsehttpResponse= null;<br />private HttpEntityhttpEntity= null;</p><p>@Override<br />public void onCreate(Bundle savedInstanceState)<br />{<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);<br />text = (TextView) findViewById(R.id.text);<br />button = (Button) findViewById(R.id.button);<br />button.setOnClickListener(new OnClickListener() {<br />@Override<br />public void onClick(View v)<br />{<br />// 產生一個請求對象,參數就是地址<br />HttpGet httpGet = new HttpGet("http://www.baidu.com");<br />// 產生Http用戶端<br />HttpClient httpClient = new DefaultHttpClient();<br />InputStream inputStream = null;<br />// 使用HTTP用戶端發送請求對象<br />try<br />{<br />// 發送請求的響應<br />httpResponse = httpClient.execute(httpGet);<br />// 代表接收的http訊息,伺服器返回的訊息都在httpEntity<br />httpEntity = httpResponse.getEntity();<br />if(httpResponse.getStatusLine().getStatusCode() == 200){<br />inputStream = httpEntity.getContent();<br />BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));<br />String result = "";<br />String line = "";<br />while ((line = reader.readLine()) != null)<br />{<br />result = result + line;<br />}</p><p>text.setText(result);<br />}<br />}<br />catch (ClientProtocolException e)<br />{<br />e.printStackTrace();<br />}<br />catch (Exception e)<br />{<br />e.printStackTrace();<br />}<br />finally<br />{<br />try<br />{<br />inputStream.close();<br />}<br />catch (IOException e)<br />{<br />e.printStackTrace();<br />}<br />}<br />}<br />});<br />}<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.