android-async-http第三方包的使用

今后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"/>
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值