首先添加依赖
implementation 'com.squareup.okhttp3:okhttp:3.5.0'
implementation 'com.google.code.gson:gson:2.8.0'
然后直接工具类
package com.pw.okhttps.httpUtils;
import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import androidx.annotation.NonNull;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* 网络请求管理类
* 2019年01月30日09:14:30
*
* @author pw
*/
public class BstRequestClient {
private static final String TAG = "BST.REQUEST==";
private static BstRequestClient mInstance;
private OkHttpClient mOkHttpClient;//okHttpClient 实例
private Handler okHttpHandler;//全局处理子线程和M主线程通信
private Gson mGson;
private BstRequestClient() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS) //设置超时时间
.readTimeout(15, TimeUnit.SECONDS) //设置读取超时时间
.writeTimeout(15, TimeUnit.SECONDS); //设置写入超时时间;
//支持HTTPS请求,跳过证书验证
clientBuilder.sslSocketFactory(createSSLSocketFactory());
clientBuilder.hostnameVerifier(new HostnameVerifier() {
@SuppressLint("BadHostnameVerifier")
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
//初始化Handler
okHttpHandler = new Handler();
mGson = new Gson();
mOkHttpClient = clientBuilder.build();
}
public static BstRequestClient getInstance() {
if (mInstance == null) {
synchronized (BstRequestClient.class) {
if (mInstance == null) {
mInstance = new BstRequestClient();
}
}
}
return mInstance;
}
/**
* 设置OKHTTP的配置参数
*
* @param connectTime 连接超时时间
* @param readTime 读取超时时间
* @param writeTime 写入超时时间
*/
public void setHttpConfig(int connectTime, int readTime, int writeTime) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.connectTimeout(connectTime, TimeUnit.SECONDS)//设置超时时间
.readTimeout(readTime, TimeUnit.SECONDS)//设置读取超时时间
.writeTimeout(writeTime, TimeUnit.SECONDS);//设置写入超时时间;
//支持HTTPS请求,跳过证书验证
clientBuilder.sslSocketFactory(createSSLSocketFactory());
clientBuilder.hostnameVerifier(new HostnameVerifier() {
@SuppressLint("BadHostnameVerifier")
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
mOkHttpClient = clientBuilder.build();
}
public Gson getGson() {
if (mGson == null)
synchronized (BstRequestClient.class) {
if (mGson == null)
mGson = new Gson();
}
return mGson;
}
/**
* post请求 (此方法不推荐使用)
*
* @param url
* @param params
* @param callBack
*/
@Deprecated
public void post(final String url, HashMap params, String sign, final RequestCallBack callBack) {
if (params != null) {
params.put("partnerName", "bst-project");
params.put("sign", sign);
}
RequestBody body = setRequestBody(params, url);
Request.Builder requestBuilder = new Request.Builder();
Request request = requestBuilder.post(body).url(Consts.APP_HOST + url).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.failed(e);
}
});
}
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
try {
String respStr = response.body().string();
Log.d(TAG + url, respStr);
JSONObject jsonObject = new JSONObject(respStr);
String code = jsonObject.getString("code");
if ("0".equals(code)) {
final String responseInfo = jsonObject.getString("info");
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.onResponse(responseInfo);
}
});
}
} else {
callBack.finish();
}
} catch (Exception e) {
if (callBack != null) {
callBack.finish();
}
e.printStackTrace();
}
}
});
}
/**
* post请求 (升级版)
*
* @param url
* @param datas
* @param callBack
*/
public void post(final String url, String datas, String sign, final RequestCallBack callBack) {
HashMap params = new HashMap<>();
params.put("partnerName", "bst-project");
params.put("sign", sign);
params.put("param", datas);
RequestBody body = setRequestBody(params, url);
Request.Builder requestBuilder = new Request.Builder();
Request request = requestBuilder.post(body).url(Consts.APP_HOST + url).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.failed(e);
}
});
}
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
try {
String respStr = response.body().string();
Log.e(TAG + url, respStr);
JSONObject jsonObject = new JSONObject(respStr);
String code = jsonObject.getString("code");
if ("0".equals(code)) {
final String responseInfo = jsonObject.getString("info");
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.onResponse(responseInfo);
}
});
}
} else {
callBack.finish();
}
} catch (Exception e) {
if (callBack != null) {
callBack.finish();
}
e.printStackTrace();
}
}
});
}
/**
* post请求 (升级版) (不经过过滤)
*
* @param url
* @param datas
* @param callBack
*/
public void postNoFilt(final String url, String datas,final RequestCallBack callBack) {
HashMap params = new HashMap<>();
params.put("param", datas);
RequestBody body = setRequestBody(params, url);
Request.Builder requestBuilder = new Request.Builder();
Request request = requestBuilder.post(body).url(Consts.APP_HOST + url).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.failed(e);
}
});
}
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
try {
final String respStr = response.body().string();
Log.e(TAG + url, respStr);
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.onResponse(respStr);
}
});
}
} catch (Exception e) {
if (callBack != null) {
callBack.finish();
}
e.printStackTrace();
}
}
});
}
/**
* post请求 (升级版) {返回数据是res}
*
* @param url
* @param datas
* @param callBack
*/
public void postForRes(final String url, String datas, String sign, final RequestCallBack callBack) {
HashMap params = new HashMap<>();
params.put("partnerName", "bst-project");
params.put("sign", sign);
params.put("param", datas);
RequestBody body = setRequestBody(params, url);
Request.Builder requestBuilder = new Request.Builder();
Request request = requestBuilder.post(body).url(Consts.APP_HOST + url).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.failed(e);
}
});
}
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
try {
String respStr = response.body().string();
Log.d(TAG + url, respStr);
JSONObject jsonObject = new JSONObject(respStr);
String code = jsonObject.getString("code");
if ("0".equals(code)) {
final String responseInfo = jsonObject.getString("res");
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.onResponse(responseInfo);
}
});
}
}
} catch (Exception e) {
if (callBack != null) {
callBack.finish();
}
e.printStackTrace();
}
}
});
}
/**
* get请求
*
* @param url
* @param params
* @param sign
* @param callBack
*/
public void get(String url, HashMap params, String sign, final RequestCallBack callBack) {
if (params != null) {
params.put("partnerName", "bst-project");
params.put("sign", sign); //跟后台沟通,需要就拼接,不需要就gone掉
}
String realParam = appendParams(url, params);
Request.Builder builder = new Request.Builder();
Request request = builder.get().url(realParam).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.failed(e);
}
});
}
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
try {
Log.d(TAG, response.body().string());
JSONObject jsonObject = new JSONObject(response.body().string());
String code = jsonObject.getString("code");
if ("0".equals(code)) {
final String responseInfo = jsonObject.getString("info");
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.onResponse(responseInfo);
}
});
}
}
} catch (Exception e) {
if (callBack != null) {
callBack.finish();
}
e.printStackTrace();
}
}
});
}
/**
* get请求(直接url拼接好的,并且没有code,全部回调过去)
*
* @param url
* @param callBack
*/
public void get(String url, final RequestCallBack callBack) {
Request.Builder builder = new Request.Builder();
Request request = builder.get().url(url).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull final IOException e) {
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.failed(e);
}
});
}
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
try {
final String res = response.body().string();
if (callBack != null) {
okHttpHandler.post(new Runnable() {
@Override
public void run() {
callBack.finish();
callBack.onResponse(res);
}
});
}
} catch (IOException e) {
if (callBack != null) {
callBack.finish();
}
e.printStackTrace();
}
}
});
}
/**
* get请求拼接参数
*
* @param url 地址
* @param params 参数
* @return string 拼接后的真实参数
*/
private String appendParams(String url, Map params) {
if (url != null && params != null && !params.isEmpty()) {
Uri.Builder builder = Uri.parse(url).buildUpon();
Set keys = params.keySet();
Iterator iterator = keys.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
builder.appendQueryParameter(key, params.get(key));
}
return builder.build().toString();
} else {
return url;
}
}
/**
* post的请求参数,构造RequestBody
*
* @param BodyParams 请求参数
* @return RequestBody
*/
private RequestBody setRequestBody(Map BodyParams, String url) {
RequestBody body;
okhttp3.FormBody.Builder formEncodingBuilder = new okhttp3.FormBody.Builder();
if (BodyParams != null) {
Iterator iterator = BodyParams.keySet().iterator();
String key;
while (iterator.hasNext()) {
key = iterator.next();
formEncodingBuilder.add(key, BodyParams.get(key));
Log.e("post Params==" + url, BodyParams.get(key));
}
}
body = formEncodingBuilder.build();
return body;
}
/**
* 生成安全套接字工厂,用于https请求的证书跳过
*
* @return SSLSocketFactory
*/
private SSLSocketFactory createSSLSocketFactory() {
SSLSocketFactory ssfFactory = null;
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());
ssfFactory = sc.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return ssfFactory;
}
/**
* 用于信任所有证书
*/
private class TrustAllCerts implements X509TrustManager {
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
}
@SuppressLint("TrustAllX509TrustManager")
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
}
然后是存放url的类
package com.pw.okhttps.httpUtils;
/**
* author: pw on 2019/1/29 17:08
* email: [email protected]
*/
public class Consts {
public static final String APP_HOST = "https://api.douban.com/v2/movie/top250?start=0&count=10";
}
然后是回掉的接口
package com.pw.okhttps.httpUtils;
public interface RequestCallBack {
/**
* 成功回调
*
* @param response
*/
void onResponse(String response);
/**
* 请求失败的时候
*
* @param e
*/
void failed(Exception e);
/**
* 完成回调,不管失败还是成功都调用此方法,以便dismiss dialog等共同的操作
*/
void finish();
}
然后是我的javabean
package com.pw.okhttps.httpUtils;
import java.util.List;
/**
* author: pw on 2019/1/29 17:35
* email: [email protected]
*/
public class MyBeans {
/**
* count : 10
* start : 0
* total : 250
* subjects : [{"rating":{"max":10,"average":9.6,"stars":"50","min":0},"genres":["犯罪","剧情"],"title":"肖申克的救赎","casts":[{"alt":"https://movie.douban.com/celebrity/1054521/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg"},"name":"蒂姆·罗宾斯","id":"1054521"},{"alt":"https://movie.douban.com/celebrity/1054534/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg"},"name":"摩根·弗里曼","id":"1054534"},{"alt":"https://movie.douban.com/celebrity/1041179/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg"},"name":"鲍勃·冈顿","id":"1041179"}],"collect_count":1633014,"original_title":"The Shawshank Redemption","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1047973/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg"},"name":"弗兰克·德拉邦特","id":"1047973"}],"year":"1994","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg"},"alt":"https://movie.douban.com/subject/1292052/","id":"1292052"},{"rating":{"max":10,"average":9.6,"stars":"50","min":0},"genres":["剧情","爱情","同性"],"title":"霸王别姬","casts":[{"alt":"https://movie.douban.com/celebrity/1003494/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p67.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p67.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p67.jpg"},"name":"张国荣","id":"1003494"},{"alt":"https://movie.douban.com/celebrity/1050265/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46345.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46345.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46345.jpg"},"name":"张丰毅","id":"1050265"},{"alt":"https://movie.douban.com/celebrity/1035641/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1399268395.47.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1399268395.47.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1399268395.47.jpg"},"name":"巩俐","id":"1035641"}],"collect_count":1303574,"original_title":"霸王别姬","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1023040/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1451727734.81.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1451727734.81.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1451727734.81.jpg"},"name":"陈凯歌","id":"1023040"}],"year":"1993","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1910813120.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1910813120.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1910813120.jpg"},"alt":"https://movie.douban.com/subject/1291546/","id":"1291546"},{"rating":{"max":10,"average":9.4,"stars":"50","min":0},"genres":["剧情","动作","犯罪"],"title":"这个杀手不太冷","casts":[{"alt":"https://movie.douban.com/celebrity/1025182/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p8833.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p8833.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p8833.jpg"},"name":"让·雷诺","id":"1025182"},{"alt":"https://movie.douban.com/celebrity/1054454/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p2274.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p2274.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p2274.jpg"},"name":"娜塔莉·波特曼","id":"1054454"},{"alt":"https://movie.douban.com/celebrity/1010507/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33896.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33896.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33896.jpg"},"name":"加里·奥德曼","id":"1010507"}],"collect_count":1696777,"original_title":"Léon","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1031876/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33301.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33301.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33301.jpg"},"name":"吕克·贝松","id":"1031876"}],"year":"1994","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p511118051.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p511118051.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p511118051.jpg"},"alt":"https://movie.douban.com/subject/1295644/","id":"1295644"},{"rating":{"max":10,"average":9.4,"stars":"50","min":0},"genres":["剧情","爱情"],"title":"阿甘正传","casts":[{"alt":"https://movie.douban.com/celebrity/1054450/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p28603.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p28603.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p28603.jpg"},"name":"汤姆·汉克斯","id":"1054450"},{"alt":"https://movie.douban.com/celebrity/1002676/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1537890386.77.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1537890386.77.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1537890386.77.jpg"},"name":"罗宾·怀特","id":"1002676"},{"alt":"https://movie.douban.com/celebrity/1031848/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1345.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1345.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1345.jpg"},"name":"加里·西尼斯","id":"1031848"}],"collect_count":1428360,"original_title":"Forrest Gump","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1053564/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p505.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p505.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p505.jpg"},"name":"罗伯特·泽米吉斯","id":"1053564"}],"year":"1994","images":{"small":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p510876377.jpg","large":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p510876377.jpg","medium":"https://img1.doubanio.com/view/photo/s_ratio_poster/public/p510876377.jpg"},"alt":"https://movie.douban.com/subject/1292720/","id":"1292720"},{"rating":{"max":10,"average":9.5,"stars":"50","min":0},"genres":["剧情","喜剧","爱情"],"title":"美丽人生","casts":[{"alt":"https://movie.douban.com/celebrity/1041004/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p26764.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p26764.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p26764.jpg"},"name":"罗伯托·贝尼尼","id":"1041004"},{"alt":"https://movie.douban.com/celebrity/1000375/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9548.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9548.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9548.jpg"},"name":"尼可莱塔·布拉斯基","id":"1000375"},{"alt":"https://movie.douban.com/celebrity/1000368/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45590.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45590.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45590.jpg"},"name":"乔治·坎塔里尼","id":"1000368"}],"collect_count":752605,"original_title":"La vita è bella","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1041004/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p26764.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p26764.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p26764.jpg"},"name":"罗伯托·贝尼尼","id":"1041004"}],"year":"1997","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p510861873.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p510861873.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p510861873.jpg"},"alt":"https://movie.douban.com/subject/1292063/","id":"1292063"},{"rating":{"max":10,"average":9.3,"stars":"50","min":0},"genres":["剧情","爱情","灾难"],"title":"泰坦尼克号","casts":[{"alt":"https://movie.douban.com/celebrity/1041029/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p470.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p470.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p470.jpg"},"name":"莱昂纳多·迪卡普里奥","id":"1041029"},{"alt":"https://movie.douban.com/celebrity/1054446/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p53358.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p53358.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p53358.jpg"},"name":"凯特·温丝莱特","id":"1054446"},{"alt":"https://movie.douban.com/celebrity/1031864/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45186.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45186.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p45186.jpg"},"name":"比利·赞恩","id":"1031864"}],"collect_count":1322308,"original_title":"Titanic","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1022571/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33715.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33715.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p33715.jpg"},"name":"詹姆斯·卡梅隆","id":"1022571"}],"year":"1997","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p457760035.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p457760035.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p457760035.jpg"},"alt":"https://movie.douban.com/subject/1292722/","id":"1292722"},{"rating":{"max":10,"average":9.3,"stars":"50","min":0},"genres":["剧情","动画","奇幻"],"title":"千与千寻","casts":[{"alt":"https://movie.douban.com/celebrity/1023337/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1463193210.13.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1463193210.13.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1463193210.13.jpg"},"name":"柊瑠美","id":"1023337"},{"alt":"https://movie.douban.com/celebrity/1005438/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44986.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44986.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44986.jpg"},"name":"入野自由","id":"1005438"},{"alt":"https://movie.douban.com/celebrity/1045797/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1376151005.51.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1376151005.51.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1376151005.51.jpg"},"name":"夏木真理","id":"1045797"}],"collect_count":1217407,"original_title":"千と千尋の神隠し","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1054439/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p616.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p616.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p616.jpg"},"name":"宫崎骏","id":"1054439"}],"year":"2001","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1606727862.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1606727862.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1606727862.jpg"},"alt":"https://movie.douban.com/subject/1291561/","id":"1291561"},{"rating":{"max":10,"average":9.5,"stars":"50","min":0},"genres":["剧情","历史","战争"],"title":"辛德勒的名单","casts":[{"alt":"https://movie.douban.com/celebrity/1031220/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44906.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44906.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p44906.jpg"},"name":"连姆·尼森","id":"1031220"},{"alt":"https://movie.douban.com/celebrity/1054393/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1374649659.58.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1374649659.58.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1374649659.58.jpg"},"name":"本·金斯利","id":"1054393"},{"alt":"https://movie.douban.com/celebrity/1006956/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p28941.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p28941.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p28941.jpg"},"name":"拉尔夫·费因斯","id":"1006956"}],"collect_count":702163,"original_title":"Schindler's List","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1054440/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34602.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34602.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34602.jpg"},"name":"史蒂文·斯皮尔伯格","id":"1054440"}],"year":"1993","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p492406163.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p492406163.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p492406163.jpg"},"alt":"https://movie.douban.com/subject/1295124/","id":"1295124"},{"rating":{"max":10,"average":9.3,"stars":"50","min":0},"genres":["剧情","科幻","悬疑"],"title":"盗梦空间","casts":[{"alt":"https://movie.douban.com/celebrity/1041029/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p470.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p470.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p470.jpg"},"name":"莱昂纳多·迪卡普里奥","id":"1041029"},{"alt":"https://movie.douban.com/celebrity/1101703/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3517.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3517.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p3517.jpg"},"name":"约瑟夫·高登-莱维特","id":"1101703"},{"alt":"https://movie.douban.com/celebrity/1012520/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p118.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p118.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p118.jpg"},"name":"艾伦·佩吉","id":"1012520"}],"collect_count":1446816,"original_title":"Inception","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1054524/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p673.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p673.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p673.jpg"},"name":"克里斯托弗·诺兰","id":"1054524"}],"year":"2010","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p513344864.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p513344864.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p513344864.jpg"},"alt":"https://movie.douban.com/subject/3541415/","id":"3541415"},{"rating":{"max":10,"average":9.3,"stars":"50","min":0},"genres":["爱情","科幻","动画"],"title":"机器人总动员","casts":[{"alt":"https://movie.douban.com/celebrity/1009535/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13028.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13028.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p13028.jpg"},"name":"本·贝尔特","id":"1009535"},{"alt":"https://movie.douban.com/celebrity/1000389/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519794715.93.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519794715.93.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1519794715.93.jpg"},"name":"艾丽莎·奈特","id":"1000389"},{"alt":"https://movie.douban.com/celebrity/1018022/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p31068.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p31068.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p31068.jpg"},"name":"杰夫·格尔林","id":"1018022"}],"collect_count":912054,"original_title":"WALL·E","subtype":"movie","directors":[{"alt":"https://movie.douban.com/celebrity/1036450/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1467359656.96.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1467359656.96.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1467359656.96.jpg"},"name":"安德鲁·斯坦顿","id":"1036450"}],"year":"2008","images":{"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1461851991.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1461851991.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p1461851991.jpg"},"alt":"https://movie.douban.com/subject/2131459/","id":"2131459"}]
* title : 豆瓣电影Top250
*/
private int count;
private int start;
private int total;
private String title;
private List subjects;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List getSubjects() {
return subjects;
}
public void setSubjects(List subjects) {
this.subjects = subjects;
}
public static class SubjectsBean {
/**
* rating : {"max":10,"average":9.6,"stars":"50","min":0}
* genres : ["犯罪","剧情"]
* title : 肖申克的救赎
* casts : [{"alt":"https://movie.douban.com/celebrity/1054521/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg"},"name":"蒂姆·罗宾斯","id":"1054521"},{"alt":"https://movie.douban.com/celebrity/1054534/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34642.jpg"},"name":"摩根·弗里曼","id":"1054534"},{"alt":"https://movie.douban.com/celebrity/1041179/","avatars":{"small":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg","large":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg","medium":"https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p5837.jpg"},"name":"鲍勃·冈顿","id":"1041179"}]
* collect_count : 1633014
* original_title : The Shawshank Redemption
* subtype : movie
* directors : [{"alt":"https://movie.douban.com/celebrity/1047973/","avatars":{"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg"},"name":"弗兰克·德拉邦特","id":"1047973"}]
* year : 1994
* images : {"small":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg","large":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg","medium":"https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg"}
* alt : https://movie.douban.com/subject/1292052/
* id : 1292052
*/
private RatingBean rating;
private String title;
private int collect_count;
private String original_title;
private String subtype;
private String year;
private ImagesBean images;
private String alt;
private String id;
private List genres;
private List casts;
private List directors;
public RatingBean getRating() {
return rating;
}
public void setRating(RatingBean rating) {
this.rating = rating;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getCollect_count() {
return collect_count;
}
public void setCollect_count(int collect_count) {
this.collect_count = collect_count;
}
public String getOriginal_title() {
return original_title;
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
}
public String getSubtype() {
return subtype;
}
public void setSubtype(String subtype) {
this.subtype = subtype;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public ImagesBean getImages() {
return images;
}
public void setImages(ImagesBean images) {
this.images = images;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List getGenres() {
return genres;
}
public void setGenres(List genres) {
this.genres = genres;
}
public List getCasts() {
return casts;
}
public void setCasts(List casts) {
this.casts = casts;
}
public List getDirectors() {
return directors;
}
public void setDirectors(List directors) {
this.directors = directors;
}
public static class RatingBean {
/**
* max : 10
* average : 9.6
* stars : 50
* min : 0
*/
private int max;
private double average;
private String stars;
private int min;
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
public String getStars() {
return stars;
}
public void setStars(String stars) {
this.stars = stars;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
}
public static class ImagesBean {
/**
* small : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
* large : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
* medium : https://img3.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg
*/
private String small;
private String large;
private String medium;
public String getSmall() {
return small;
}
public void setSmall(String small) {
this.small = small;
}
public String getLarge() {
return large;
}
public void setLarge(String large) {
this.large = large;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
}
public static class CastsBean {
/**
* alt : https://movie.douban.com/celebrity/1054521/
* avatars : {"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg"}
* name : 蒂姆·罗宾斯
* id : 1054521
*/
private String alt;
private AvatarsBean avatars;
private String name;
private String id;
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public AvatarsBean getAvatars() {
return avatars;
}
public void setAvatars(AvatarsBean avatars) {
this.avatars = avatars;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public static class AvatarsBean {
/**
* small : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg
* large : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg
* medium : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p17525.jpg
*/
private String small;
private String large;
private String medium;
public String getSmall() {
return small;
}
public void setSmall(String small) {
this.small = small;
}
public String getLarge() {
return large;
}
public void setLarge(String large) {
this.large = large;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
}
}
public static class DirectorsBean {
/**
* alt : https://movie.douban.com/celebrity/1047973/
* avatars : {"small":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","large":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg","medium":"https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg"}
* name : 弗兰克·德拉邦特
* id : 1047973
*/
private String alt;
private AvatarsBeanX avatars;
private String name;
private String id;
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public AvatarsBeanX getAvatars() {
return avatars;
}
public void setAvatars(AvatarsBeanX avatars) {
this.avatars = avatars;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public static class AvatarsBeanX {
/**
* small : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg
* large : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg
* medium : https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p230.jpg
*/
private String small;
private String large;
private String medium;
public String getSmall() {
return small;
}
public void setSmall(String small) {
this.small = small;
}
public String getLarge() {
return large;
}
public void setLarge(String large) {
this.large = large;
}
public String getMedium() {
return medium;
}
public void setMedium(String medium) {
this.medium = medium;
}
}
}
}
}
然后是我们使用
package com.pw.okhttps;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.pw.okhttps.httpUtils.BstRequestClient;
import com.pw.okhttps.httpUtils.Consts;
import com.pw.okhttps.httpUtils.MyBeans;
import com.pw.okhttps.httpUtils.RequestCallBack;
import org.json.JSONException;
import org.json.JSONObject;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button btn_start;
private LinearLayout llpro_gress;
private TextView tv_contents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btn_start = findViewById(R.id.btn_start);
llpro_gress = findViewById(R.id.ll_progress);
tv_contents = findViewById(R.id.tv_contents);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
llpro_gress.setVisibility(View.VISIBLE);
//get 请求
getNet();
// post 请求
// postNet();
}
});
}
/**
* Consts.APP_HOST 是 url
*/
private void getNet() {
BstRequestClient.getInstance().get(Consts.APP_HOST, new RequestCallBack() {
@Override
public void onResponse(String response) {
try {
MyBeans myBeans = BstRequestClient.getInstance().getGson().fromJson(response, MyBeans.class);
tv_contents.setText(myBeans.getTitle().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failed(Exception e) {
e.printStackTrace();
}
@Override
public void finish() {
llpro_gress.setVisibility(View.GONE);
}
});
}
private void postNet() {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("shop_id", "1");
BstRequestClient.getInstance().post(Consts.APP_HOST, jsonObject.toString(), "", new RequestCallBack() {
@Override
public void onResponse(String response) {
try {
MyBeans myBeans = BstRequestClient.getInstance().getGson().fromJson(response, MyBeans.class);
tv_contents.setText(myBeans.getTotal() + myBeans.getCount());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failed(Exception e) {
Log.e("a", "failed: " + e.getMessage());
}
@Override
public void finish() {
llpro_gress.setVisibility(View.GONE);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
上面写了get和post请求,一般post请求最为常用,我这里是用的get请求
然后是布局
然后别忘了在清单文件添加权限