首页 > 代码库 > android 学习随笔十二(网络:使用异步HttpClient框架)
android 学习随笔十二(网络:使用异步HttpClient框架)
使用异步HttpClient框架发送get、post请求
在https://github.com/ 搜索 asyn-http https://github.com/search?utf8=?&q=asyn-http 下载 loopj/android-async-http
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(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click1(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(); String pass = et_pass.getText().toString(); String path = "http://169.254.244.136/Web2/servlet/Login"; //使用异步HttpClient发送get请求 AsyncHttpClient client = new AsyncHttpClient(); //把要提交的数据封装在rp对象中 RequestParams rp = new RequestParams(); rp.put("name", name); rp.put("pass", pass); //发送get请求 client.get(path, rp, new MyHandler()); } class MyHandler extends AsyncHttpResponseHandler{ //请求成功时回调,相应码为200 @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(MainActivity.this, new String(responseBody), 0).show(); } //请求失败时回调,相应码不为200 @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(MainActivity.this, "请求失败", 0).show(); } } public void click2(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(); String pass = et_pass.getText().toString(); String path = "http://169.254.244.136/Web2/servlet/Login"; //使用异步HttpClient发送post请求 AsyncHttpClient client = new AsyncHttpClient(); //把要提交的数据封装在rp对象中 RequestParams rp = new RequestParams(); rp.put("name", name); rp.put("pass", pass); //发送get请求 client.post(path, rp, new MyHandler()); } }
android 学习随笔十二(网络:使用异步HttpClient框架)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。