简单学习Android联网框架OkHttpUtils及AsycHttpClient

==OkHttpUtils联网请求==

1、添加依赖

implementation 'com.zhy:okhttputils:2.6.2'

2、请求数据

String url = "";
OkHttpUtils.post().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                e.printStackTrace();
                //吐司:联网失败
            }

            @Override
            public void onResponse(String response, int id) {
                //在这里解析返回数据
                ......
            }
        });

带参数的请求

OkHttpUtils.post().url(url)
    .addParams("username", "haojundd")
    .addParams("password", "7474740")
    .build().execute(......);

==AsycHttpClient联网请求==

1、添加依赖

repositories {
  mavenCentral()
}
dependencies {
  implementation 'com.loopj.android:android-async-http:1.4.9'
}

或者直接下载jar包放进lib然后右击buildpath
https://search.maven.org/remotecontent?filepath=com/loopj/android/android-async-http/1.4.9/android-async-http-1.4.9.jar
2、请求数据

AsyncHttpClient client=new AsyncHttpClient();
        String url = "";
        client.post(url,new AsyncHttpResponseHandler(){
            @Override
            public void onFailure(Throwable error, String content) {
                //吐司:联网失败
            }

            @Override
            public void onSuccess(String response) {
                //在这里解析返回数据
                ......
            }
        });

猜你喜欢

转载自blog.csdn.net/qq_39652726/article/details/80422396
今日推荐