2018-03-10 android下提交数据(request)
2018-03-12 本文已影响0人
紫杉叶子
笔记如下
使用 开源框架 -------android-async-http-master
- 首先开启权限
<uses-permission android:name="android.permission.INTERNET"/>
- get
AsyncHttpClient client = new AsyncHttpClient();
client.get(path, new AsyncHttpResponseHandler() {
//请求成功会被调用的
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//System.out.println(new String(responseBody));
tv_status.setText(new String(responseBody));
}
//请求失败时候会被调用的
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
error.printStackTrace(System.out);
}
});
- post
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("number",number);
params.add("pwd",pwd);
client.post(path,params,new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//System.out.println(responseBody);
tv_status.setText(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
error.printStackTrace(System.out);
}
});