安卓第三方插件async-http

一.什么是安卓async-http
async-http是用于请求网络的工具,是一个第三方的网络插件,可以方便访问网络。
二.如何使用async-http
1.首先添加依赖包,下载第三方插件

compile 'com.loopj.android:android-async-http:1.4.9'

2.然后创建类写访问网络方法

package com.example.ll.king;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * Created by ll on 2018/4/10.
 */

public class Adk {
    //写的是http网络接口,静态是因为是这一段http是一直不变的
    private static final String BASE_URL = "http://103.244.59.105:8014/paopaoserver/";

    //new一个私有的AsyncHttpClient
    private static AsyncHttpClient client = new AsyncHttpClient();

    //这是一个get的回调方法,所以是公共类
    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    //post回调方法
    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    //http网络访问的公式:访问的网址=BASE_URL(固定不变的) + relativeUrl
    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }

}

3.接着写好xml的布局


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ll.king.MainActivity">
<EditText
    android:id="@+id/xiueru"
    android:layout_width="match_parent"
    android:layout_height="50dp" />
    <Button
        android:text="测试一下"
        android:id="@+id/btn_ll"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:id="@+id/add_btn"
        android:text="删除一条数据"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:text="访问网络"
        android:id="@+id/cmc"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <Button
        android:text="跑跑APP"
        android:id="@+id/psp"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
LinearLayout>

4.主页面进行网络访问(键值对形式)

package com.example.ll.king;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //定义控件
    private Button textbtn;
    private Button addbtn;
    private EditText editText;
    private Button btn;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //绑定控件的方法
        BindId();
    }
    //绑定控件
    private void BindId() {
        textbtn = findViewById(R.id.btn_ll);
        textbtn.setOnClickListener(this);
        addbtn = findViewById(R.id.add_btn);
        addbtn.setOnClickListener(this);
        editText = findViewById(R.id.xiueru);
        btn = findViewById(R.id.cmc);
        btn.setOnClickListener(this);
        btn2 = findViewById(R.id.psp);
        btn2.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ll:
                String name = editText.getText().toString();

                Uri uri = Uri.parse("content://com.ll.Mys.Rng");
                ContentResolver resolver = getContentResolver();
                ContentValues contentValues = new ContentValues();
                contentValues.put("name", name);
                resolver.insert(uri, contentValues);

                break;
            case R.id.add_btn:
                String name1 = editText.getText().toString();

                Uri uri1 = Uri.parse("content://com.ll.Mys.Rng");
                ContentResolver resolver1 = getContentResolver();

                resolver1.delete(uri1, "name=?", new String[]{name1});

                break;
            case R.id.cmc:
                AsyncHttpClient client = new AsyncHttpClient();
                client.get("https://www.csdn.net/", new TextHttpResponseHandler() {
                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        Toast.makeText(MainActivity.this, "访问网络失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, String responseString) {
                        Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();

                    }
                });
                break;
            case R.id.psp:
                RequestParams requestParams = new RequestParams();
                requestParams.put("params", "{\"page\":1,\"page_count\":10}");
                //以键值对的形式进行访问
                Adk.get("articles", requestParams, new TextHttpResponseHandler() {
                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        Toast.makeText(MainActivity.this, "访问网络失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, String responseString) {
                        Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }

    }
}

你可能感兴趣的:(安卓第三方插件async-http)