zoukankan      html  css  js  c++  java
  • 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());
        }
        
    }
  • 相关阅读:
    react路由组件&&非路由组件
    react函数式组件(非路由组件)实现路由跳转
    react使用antd组件递归实现左侧菜单导航树
    【LeetCode】65. Valid Number
    【LeetCode】66. Plus One (2 solutions)
    【LeetCode】68. Text Justification
    【LeetCode】69. Sqrt(x) (2 solutions)
    【LeetCode】72. Edit Distance
    【LeetCode】73. Set Matrix Zeroes (2 solutions)
    【LeetCode】76. Minimum Window Substring
  • 原文地址:https://www.cnblogs.com/ecollab/p/5896086.html
Copyright © 2011-2022 走看看