android:开源AsyncHttpClient

通过开源的AsyncHttpClient进行网络请求

注意:需要引入以下两个jar包

android-async-http-x.x.x jar  ,httpclient-x.x.x.jar包  

布局文 activity_main.xml




    

        
            
            
        

        
            
            
        

        
            

java文件
MainActivity.java
 
   
 
   
package com.smart.asynchttp.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends AppCompatActivity {
    private EditText et_username;
    private EditText et_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_username = (EditText)findViewById(R.id.et_username);
        et_password = (EditText)findViewById(R.id.et_password);
    }

    public void onClick(View v) {
        if(v.getId() == R.id.bt_cannel) {
            loginPost();
        } else  if(v.getId() == R.id.bt_submit) {
            loginGet();
        }
    }
    /**
     * GET方式请求数据
     */
    public void loginGet() {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String urlPath = "http://192.168.1.6:8080/androidweb/login?username="+username+"&password="+password;
        //创建异步http请求对象
        AsyncHttpClient client = new AsyncHttpClient();
        //通过Post的方式获取请求
        client.get(urlPath, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//                showToast(new String(responseBody));
                Toast.makeText(getApplicationContext(),new String(responseBody),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                showToast();
                Toast.makeText(getApplicationContext(),"请求失败",Toast.LENGTH_LONG).show();
            }
        });
    }


    /**
     * Post方式请求数据,为安全起见都用post
     */
    public void loginPost() {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String urlPath = "http://192.168.1.6:8080/androidweb/login";
       //创建异步http请求对象
        AsyncHttpClient client = new AsyncHttpClient();
        //设置POST请求参数
        RequestParams params = new RequestParams();
        params.put("username",username);
        params.put("password",password);
        //Post请求
        client.post(urlPath,params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Toast.makeText(getApplicationContext(),new String(responseBody),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//                showToast();
                Toast.makeText(getApplicationContext(),"请求失败",Toast.LENGTH_LONG).show();
            }
        });
    }


}

 
   

 
  


你可能感兴趣的:(android)