首先在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中了
时间: 2024-10-09 17:35:54