android中HttpClient的應用(POST方法)

來源:互聯網
上載者:User

標籤:

首先在http://hc.apache.org/downloads.cgi下載HttpClient包

直接看代碼

  1 import android.os.Handler;  2 import android.os.Message;  3 import android.support.v7.app.AppCompatActivity;  4 import android.os.Bundle;  5 import android.util.Log;  6 import android.view.View;  7 import android.widget.TextView;  8   9 import com.google.gson.Gson; 10 import com.google.gson.reflect.TypeToken; 11  12 import org.apache.http.HttpEntity; 13 import org.apache.http.HttpResponse; 14 import org.apache.http.HttpStatus; 15 import org.apache.http.NameValuePair; 16 import org.apache.http.client.ClientProtocolException; 17 import org.apache.http.client.HttpClient; 18 import org.apache.http.client.entity.UrlEncodedFormEntity; 19 import org.apache.http.client.methods.HttpGet; 20 import org.apache.http.client.methods.HttpPost; 21 import org.apache.http.client.utils.URLEncodedUtils; 22 import org.apache.http.impl.client.DefaultHttpClient; 23 import org.apache.http.message.BasicNameValuePair; 24 import org.apache.http.protocol.HTTP; 25 import org.apache.http.util.EntityUtils; 26  27 import java.io.IOException; 28 import java.io.UnsupportedEncodingException; 29 import java.lang.reflect.Type; 30 import java.util.ArrayList; 31 import java.util.LinkedList; 32 import java.util.List; 33  34 public class MainActivity extends AppCompatActivity { 35  36     @Override 37     protected void onCreate(Bundle savedInstanceState) { 38         super.onCreate(savedInstanceState); 39         setContentView(R.layout.activity_main); 40         // 開啟一個子線程,進行網路操作,等待有返回結果,使用handler通知UI 41         new Thread(networkTask).start(); 42  43     } 44  45     Handler handler = new Handler() { 46         @Override 47         public void handleMessage(Message msg) { 48             super.handleMessage(msg); 49             Bundle data = msg.getData(); 50             String val = data.getString("value"); 51             Log.i("mylog", "請求結果為-->" + val); 52             // TODO 53             // UI介面的更新等相關操作 54         } 55     }; 56  57     /** 58      * 網路操作相關的子線程 59      */ 60     Runnable networkTask = new Runnable() { 61         @Override 62         public void run() { 63             // TODO 64             // 在這裡進行 http request.網路請求相關操作 65             String TAG = "ABC"; 66  67             //baseUrl 68             String url = "http://192.168.8.7/insert.php"; 69  70             HttpPost request = new HttpPost(url); 71  72             //先將參數放入List,再對參數進行URL編碼 73             List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); 74             params.add(new BasicNameValuePair("firstname", "這個是安卓")); 75             params.add(new BasicNameValuePair("lastname", "這是安卓加進去的")); 76             params.add(new BasicNameValuePair("age", "66")); 77  78             try { 79                 HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); 80  81                 request.setEntity(entity); 82  83                 HttpClient client = new DefaultHttpClient(); 84  85                 HttpResponse response = client.execute(request); 86  87                 Log.e(TAG, "Response Code: " + 88                         response.getStatusLine().getStatusCode()); 89  90                 if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 91  92                     String data = EntityUtils.toString(response.getEntity()); 93  94                 } 95             } catch (IOException e) { 96                 Log.e(TAG, "~~~~~~"+e); 97                 e.printStackTrace(); 98             } 99             Message msg = new Message();100             Bundle data = new Bundle();101             data.putString("value", "請求結果");102             msg.setData(data);103             handler.sendMessage(msg);104         }105     }106 }

需要注意的是http請求在安卓3.0後就不能放在MainTread中了

android中HttpClient的應用(POST方法)

聯繫我們

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