android-async-http第三方包的使用
来源:互联网 发布:mysql二级缓存 编辑:程序博客网 时间:2024/06/04 18:07
今后android网络操作就用这个jar包了,哈哈。。。
需要android-async-http-1.4.8和httpcore-4.2.4的jar包。
这个包能解决网络操作的大部分问题,包括:
1,GET请求
2,POST请求
3,文件上传
4,下载文件
5,请求JSON数据等
布局就不写了。
先写一个Helper类
package com.jackie.android.async.http.util;import android.os.Build;import com.loopj.android.http.AsyncHttpClient;/** * 使用android-async-http-1.4.9包封装的异步HTTP请求工具类 * Created by Administrator on 2016/11/30. */public class AsyncHttpHelper { private static final AsyncHttpClient client; /** * android 客户端信息 */ private static String appUserAgent; static { client = new AsyncHttpClient(); client.setMaxRetriesAndTimeout(3, 10000); client.setUserAgent(getUserAgent()); } private static String getUserAgent() { if (appUserAgent == null || appUserAgent == "") { StringBuilder sb = new StringBuilder("jackie"); sb.append("|Android"); sb.append("|" + Build.VERSION.RELEASE);//手机系统版本 sb.append("|" + Build.MODEL);//手机型号 appUserAgent = sb.toString(); } return appUserAgent; } private AsyncHttpHelper() { } public static AsyncHttpClient getClient() { return client; }}
主界面如下:
package com.jackie.android.async.http.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.jackie.android.async.http.R;import com.jackie.android.async.http.util.AsyncHttpHelper;import com.loopj.android.http.RequestParams;import com.loopj.android.http.TextHttpResponseHandler;import org.apache.http.Header;/** * Created by Administrator on 2016/11/30. */public class MainActivity extends Activity { private Button button_register; private EditText editText_lname; private EditText editText_phone_number; private EditText editText_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_register = (Button) findViewById(R.id.btn_register); editText_lname = (EditText) findViewById(R.id.name); editText_phone_number = (EditText) findViewById(R.id.phone); editText_password = (EditText) findViewById(R.id.password); button_register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String lname = editText_lname.getText().toString(); String phone_number = editText_phone_number.getText().toString(); String password = editText_password.getText().toString(); String url = "http://192.168.1.100:8080/waimai/register"; RequestParams params = new RequestParams(); params.put("lname", lname); params.put("phone_number", phone_number); params.put("password", password); AsyncHttpHelper.getClient().post(url, params, new TextHttpResponseHandler() { @Override public void onFailure(int i, Header[] headers, String s, Throwable throwable) { Toast.makeText(MainActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void onSuccess(int i, Header[] headers, String s) { Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } }); } }); }}
不要忘了加权限:
<uses-permission android:name="android.permission.INTERNET"/>
0 0
- android-async-http第三方包的使用
- android请求网络——第三方库android-async-http的使用(1)
- android请求网络——第三方库android-async-http的使用(2)
- android请求网络——第三方库android-async-http的使用(3)
- android请求网络——第三方库android-async-http的使用(4)
- android请求网络-第三方库Android-async-http库的使用(5)
- Android请求网络--第三方库Android-async-http的使用(6)
- 安卓第三方框架 android-async-http
- android-async-http使用
- android async-http使用
- android-async-http使用
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- android打包准备:混淆第三方jar包(Gson, greenDao,sharesdk,UIL,高德地图, unity,pinyin4j,async http,JPush,EventBus等)
- android 如何使用第三方jar包
- 汇编语言——十进制显示Bx的值
- UnsatisfiedLinkError X.so is 64-bit instead of 32-bit之Android 64 bit SO 加载原理
- 升级编译binutil出现 ld access beyond end of merged section
- c#对Access数据库的操作
- Qt进制最大化最小化
- android-async-http第三方包的使用
- 关于direct path read
- sqlplus在linux中连接oracle时中文乱码解决方案办法。
- LongScreenshot代码流程概述
- 初始搭建dubbo-demo环境
- ssh积分商城开发实现通用层1.2
- 生成图形验证码
- activeMq实践,jms应用
- 网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP