3月21日 深圳 OSC 源创会正在报名中,送华为海思开发板
项目主页及简单使用方法http://loopj.com/android-async-http/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<
LinearLayout
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:orientation
=
"vertical"
tools:context
=
".MainActivity"
>
<
TextView
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:background
=
"#88ff98ff"
android:gravity
=
"center"
android:text
=
"使用Async框架"
android:textSize
=
"20sp"
/>
<
Button
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:onClick
=
"get_click"
android:text
=
"GET发送HTTP请求"
/>
<
Button
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:onClick
=
"post_click"
android:text
=
"POST发送HTTP请求"
/>
<
Button
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:onClick
=
"upload_click"
android:text
=
"上传文件"
/>
LinearLayout
>
|
activity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package
com.example.asynchttp;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.OutputStream;
import
java.net.URI;
import
java.net.URLEncoder;
import
org.apache.http.Header;
import
org.apache.http.HttpResponse;
import
com.loopj.android.http.AsyncHttpClient;
import
com.loopj.android.http.AsyncHttpResponseHandler;
import
com.loopj.android.http.RequestParams;
import
com.loopj.android.http.ResponseHandlerInterface;
import
com.loopj.android.http.TextHttpResponseHandler;
import
android.os.Bundle;
import
android.provider.MediaStore.Files;
import
android.app.Activity;
import
android.content.res.Resources;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.view.Menu;
import
android.view.View;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
{
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
public
void
get_click(View v)
throws
Exception
{
String path =
"http://192.168.1.100:8080/ServletTest/Login"
+
"?username="
+ URLEncoder.encode(
"test"
,
"utf-8"
) +
"&password="
+ URLEncoder.encode(
"123"
,
"utf-8"
);
AsyncHttpClient client =
new
AsyncHttpClient();
client.get(path,
new
AsyncHttpResponseHandler()
{
@Override
public
void
onSuccess(
int
statusCode, Header[] headers,
byte
[] responseBody)
{
Toast.makeText(MainActivity.
this
,
new
String(responseBody), Toast.LENGTH_SHORT).show();
}
@Override
public
void
onFailure(
int
statusCode, Header[] headers,
byte
[] responseBody, Throwable error)
{
Toast.makeText(MainActivity.
this
,
new
String(responseBody + error.getMessage()), Toast.LENGTH_SHORT).show();
}
});
}
public
void
post_click(View v)
throws
Exception
{
String path =
"http://192.168.1.100:8080/ServletTest/Login"
;
String username =
"test"
;
String password =
"123"
;
AsyncHttpClient client =
new
AsyncHttpClient();
RequestParams params =
new
RequestParams();
params.add(
"username"
, username);
params.add(
"password"
, password);
client.post(path, params,
new
TextHttpResponseHandler()
{
@Override
public
void
onSuccess(
int
statusCode, Header[] headers, String responseString)
{
Toast.makeText(MainActivity.
this
, responseString, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onFailure(
int
statusCode, Header[] headers, String responseString, Throwable throwable)
{
Toast.makeText(MainActivity.
this
, responseString, Toast.LENGTH_SHORT).show();
}
});
}
public
void
upload_click(View v)
throws
Exception
{
String path =
"http://192.168.1.100:8080/ServletTest/Upload"
;
AsyncHttpClient client =
new
AsyncHttpClient();
RequestParams params =
new
RequestParams();
InputStream is = getAssets().open(
"test.png"
);
params.put(
"file"
, is,
"测试.png"
);
client.post(path, params,
new
AsyncHttpResponseHandler()
{
@Override
public
void
onSuccess(
int
statusCode, Header[] headers,
byte
[] responseBody)
{
Toast.makeText(MainActivity.
this
,
"上传成功"
, Toast.LENGTH_SHORT).show();
}
@Override
public
void
onFailure(
int
statusCode, Header[] headers,
byte
[] responseBody, Throwable error)
{
Toast.makeText(MainActivity.
this
,
"上传失败"
, Toast.LENGTH_SHORT).show();
}
});
}
}
|