使用AsyncHttpClient提交数据get-post
2016-05-02 21:31
519 查看
<span style="font-size:24px;">package com.melody.httpclient; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; 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 com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; import com.melody.html.tool.Tools; import com.melody.httpclient_async.R; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); } }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //使用异步HttpClient get提交 public void onclick(View v){ //获取用户输入的用户名和密码 EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); String name = et_name.getText().toString().trim(); String pass = et_pass.getText().toString().trim(); //String path = "http://192.168.119.69:8080/web/Login"; String path = "http://192.168.1.106:8080/web/servlet/Login"; //使用异步HttpClient AsyncHttpClient client = new AsyncHttpClient(); //把要提交的数据封装在rp对象里面 RequestParams rp = new RequestParams(); rp.put("name", name); rp.put("pass", pass); //发送get请求 arg0:请求地址 arg1:请求参数 arg2:异步处理机制 client.get(path, rp,new MyHandler()); } //继承AsyncHttpResponseHandler工具类 class MyHandler extends AsyncHttpResponseHandler{ //请求成功时回调 responseBody 就是流里面的数据 public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, new String("responseBody"), 0).show(); } //请求失败时回调 public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(MainActivity.this, new String("responseBody"),0).show(); } } //使用异步HttpClient post提交 public void onclick_post(View v){ //获取用户输入的用户名和密码 EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); final String name = et_name.getText().toString().trim(); final String pass = et_pass.getText().toString().trim(); String path = "http://192.168.1.106:8080/web/servlet/Login"; //使用异步HttpClient AsyncHttpClient client = new AsyncHttpClient(); //把要提交的数据封装在rp对象里面 RequestParams rp = new RequestParams(); rp.put("name", name); rp.put("pass", pass); //发送get请求 arg0:请求地址 arg1:请求参数 arg2:异步处理机制 client.post(path, rp,new MyHandler()); } } </span>
相关文章推荐
- 聊聊Socket、TCP/IP、HTTP、FTP及网络编程
- 关于网络编程中服务器出现故障的三种情况(TCP协议)
- swift开发网络篇 - 用户登录POST JSON and header
- HttpServletRequest常用获取URL的方法
- virtualbox设置网络
- TCP长连接的思考和相关问题的实验_百万链接数测试_百万链接内核参数的调整
- 技术网站http://www.github.com/
- HTTP GET 请求在请求体中带参数的问题
- Volley相关知识
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />解释
- 网络编程--初识Volley之Request的使用
- 关于网络子网的划分
- httpClient发送key_value、json参数及文件
- C++实现简单的HTTP服务器
- 网游中的网络编程系列1:UDP vs. TCP
- eclipse https git
- bzoj 1596: [Usaco2008 Jan]电话网络
- TCP UDP分片
- IOCP模型与网络编程
- 网络访问回调