通过开源的AsyncHttpClient进行网络请求
注意:需要引入以下两个jar包
android-async-http-x.x.x jar ,httpclient-x.x.x.jar包
布局文
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.smart.httpclient.activity.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_username"
android:text="testname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="密 码:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Get请求"
android:id="@+id/bt_submit"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_cannel"
android:text="Post请求"
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
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(); } }); } }