<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
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: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.example.administrator.asynchttp.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送一个简单的GET请求"
android:id="@+id/button"
android:onClick="sendSimpleGetClick"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送一个带参数的请求"
android:onClick="sendParamsClick"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上传文件"
android:onClick="uploadClick"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载文件"
android:onClick="downloadClick"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="JSON请求"
android:onClick="jsonClick"
android:id="@+id/button5"
android:layout_below="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
MainActivity.java
package com.example.administrator.asynchttp;
import android.app.Activity;
import android.app.DownloadManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.apache.http.Header;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//发送简单的GET请求
public void sendSimpleGetClick(View view) {
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.baidu.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
super.onSuccess(i, headers, bytes);
//200 ok
String info = new String(bytes);
System.out.println(info);
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable error) {
super.onFailure(i, headers, bytes, error);
String info = new String(bytes);
System.out.println("error" + info);
}
});
}
//发送一个带参数的请求
public void sendParamsClick(View view) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username", "xiaofei");
params.put("password", "123");
client.post(this, "http://192.168.1.100:8080/user/LoginServlet", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
super.onSuccess(statusCode, headers, responseBody);
System.out.println(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
System.out.println(new String(responseBody));
}
});
}
//上传文件
public void uploadClick(View view) {
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("description", "美女图片");
//设置文件
try {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/appshare.jpg";
params.put("appshare.jpg", new File(path), "image/jpeg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
client.post(this, "http://10.0.2.2:8080/ServletUpload/UploadServlet", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
super.onSuccess(statusCode, headers, responseBody);
System.out.println(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
System.out.println(new String(responseBody));
}
});
}
//文件下载
public void downloadClick(View view) {
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://images.xuejuzi.cn/1505/1_150505092812_1.jpg", new FileAsyncHttpResponseHandler(this) {
@Override
public void onFailure(int i, Header[] headers, Throwable error, File file) {
//error
}
@Override
public void onSuccess(int i, Header[] headers, File file) {
//success
System.out.println(file.getAbsolutePath());
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/1_150505092812_1.jpg";
try {
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(path);
byte[] bytes = new byte[100];
int len = -1;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
out.close();
in.close();
System.out.println("download complete");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
//json请求
public void jsonClick(View view) {
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://192.168.1.100:8080/user/LoginServlet";
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("username","xiaofei");
jsonObject.put("password","123");
StringEntity entity=new StringEntity(jsonObject.toString());
client.post(this,url,entity,"application/json",new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
System.out.println(response.toString());
}
}
);
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}