android httpclient get post 代码
2014-08-25 10:27
357 查看
package com.marschen.httpgetandpost; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends ActionBarActivity { private EditText nameText; private EditText pwdText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText = (EditText) findViewById(R.id.nameText); pwdText = (EditText) findViewById(R.id.pwdText); button = (Button) findViewById(R.id.submitButton); button.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener { @Override public void onClick(View v) { String name = nameText.getText().toString(); String pwd = pwdText.getText().toString(); //使用GET方法向服务器发送请求 // GetThread gt = new GetThread(name, pwd); // gt.start(); //使用POST方法向服务器发送请求 PostThread pt = new PostThread(name, pwd); pt.start(); } } //该线程使用POST方法向服务器发送请求 class PostThread extends Thread { String name; String pwd; public PostThread(String name, String pwd) { this.name = name; this.pwd = pwd; } @Override public void run() { HttpClient httpClient = new DefaultHttpClient(); String url = "http://192.168.1.103:8080/s02e14.jsp"; //生成使用POST方法的请求对象 HttpPost httpPost = new HttpPost(url); //NameValuePair对象代表了一个需要发往服务器的键值对 NameValuePair pair1 = new BasicNameValuePair("name", name); NameValuePair pair2 = new BasicNameValuePair("password", pwd); //将准备好的键值对对象放置在一个List当中 ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(pair1); pairs.add(pair2); try { //创建代表请求体的对象 HttpEntity requestEntity = new UrlEncodedFormEntity(pairs); //将请求体放置在请求对象当中 httpPost.setEntity(requestEntity); //执行请求对象 try { HttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent())); String result = reader.readLine(); Log.d("HTTP", "POST:" + result); } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } } class GetThread extends Thread { String name; String pwd; public GetThread(String name, String pwd) { this.name = name; this.pwd = pwd; } @Override public void run() { HttpClient httpClient = new DefaultHttpClient(); String url = "http://192.168.1.103:8080/s02e14.jsp?name=" + name+ "&password=" + pwd; HttpGet httpGet = new HttpGet(url); try { HttpResponse response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent())); String result = reader.readLine(); Log.d("HTTP", "GET:" + result); } } catch (Exception e) { e.printStackTrace(); } } } }
相关文章推荐
- Android下通过httpClient发送GET和POST请求的实例代码
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- Java乔晓松-httpURLConnection、URL、httpClient、httpPost、httpGet之代码详解篇
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android开发之网络通信(HttpURLConnection,HttpClient,HttpGet,HttpPost)
- android get或post及HttpClient与服务器数据交互
- Android HttpClient基本使用方法!!--get和post
- Android利用HttpClient和Post与Get请求与服务器交互
- Android HttpClient get、post 访问网络
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- java/android httpclient post/get用法
- Android HttpClientConnection Get and Post
- Android_HttpClient_get请求post表单提交上传
- Android下通过httpClient发送GET和POST请求
- android HttpClient get请求与post请求工具类
- Android HttpClient(Get,Post方式)进行网络通信
- Android使用Apache HttpClient发送GET、POST请求
- android中Http和HttpClient的Get和Post方法的使用
- [Socket]利用Android下的HttpClient发送GET && POST请求
- Android HttpClient GET或者POST请求基本使用方法