Asynchttpclient

asynchttpclient

  • get:提交代码

 

   private void AsynchttpTaskGET() {
        String username = mName.getText().toString().trim();
        String password = mPwd.getText().toString().trim();
        String path = "http://192.168.0.156:8080/loginServlet/loginServlet?username=" + username + "&&password=" + password;
        //使用httpclient提交数据
        Log.e(TAG, "AsynchttpTaskGET: "+path );
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(path, new AsyncHttpResponseHandler() {
            //请求成功
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.e(TAG, "onSuccess: cc" );
                Toast.makeText(MainActivity.this, new String(responseBody), Toast.LENGTH_SHORT).show();
            }

            //请求失败
            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.e(TAG, "onFailure  ff: "+error.toString()  +"==="+statusCode);
            }
        });
    }
  • post:提交代码实现

 

    private void AsynchttpTaskPost() {
        String username = mName.getText().toString().trim();
        String password = mPwd.getText().toString().trim();
        String path = "http://192.168.0.156:8080/loginServlet/loginServlet";
        //定义请求体
        //String data = "username=" + username + "&&password=" + password;
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("username", username);
        params.put("password", password);
        Log.e(TAG, "AsynchttpTaskPost: "+path );
        client.post(path, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.e(TAG, "onSuccess: dd" );
                Toast.makeText(MainActivity.this, new String(responseBody), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.e(TAG, "onFailure   ee: "+error.toString() +"==="+statusCode);
            }
        });
    }

猜你喜欢

转载自www.cnblogs.com/nangongyibin/p/10226584.html