android:http

來源:互聯網
上載者:User

使用org.apache.http

//產生一個請求對象HttpGet httpGet = new HttpGet("http://www.baidu.com");//產生一個Http用戶端對象HttpClient httpClient = new DefaultHttpClient();//使用Http用戶端發送請求對象InputStream inputStream = null;try {    httpResponse = httpClient.execute(httpGet);    httpEntity = httpResponse.getEntity();    inputStream = httpEntity.getContent();    //檔案流操作    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));    String result = "";    String line = "";
//  StringBuilder builder = new StringBuilder();
   while((line = reader.readLine()) != null){ result = result + line;
// builder.append(s); } System.out.println(result);} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try{ inputStream.close(); } catch(Exception e){ e.printStackTrace(); } }}
/*  JSONObject jsonObject = new JSONObject(builder.toString());
        String re_username = jsonObject.getString("username");
        String re_password = jsonObject.getString("password");
        int re_user_id = jsonObject.getInt("user_id");
*/

GET和POST方式:

GET:即在baseUrl後“ ?”  “ &”加一些參數即可

POST方式:

NameValuePair nameValuePair1 = new BasicNameValuePair("name",name); //name為讀取的值  NameValuePair nameValuePair2 = new BasicNameValuePair("age",age); //age為讀取的值  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  nameValuePairs.add(nameValuePair1);  nameValuePairs.add(nameValuePair2);  //產生Entity對象  HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs);  HttpPost httpPost = new HttpPost(url); //這裡的url是baseUrl 不用拼上?key=value...  httpPost.setEntity(requestHttpEntity );  //之後的操作一樣  

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.