大家好,欢迎来到IT知识分享网。
//OkHttp 异步
//创建OKHttp
OkHttpClient client = new OkHttpClient.Builder().build();
//获取表单请求题
FormBody formBody = new FormBody.Builder().build();
//获取Request对象
final Request request = new Request.Builder()
.method("POST",formBody)
.url(Constant.LOGIN_URL+"?phone=" + userName + "&pwd=" + password)
.build();
//请求
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//回调错误数据
String responseData = e.getMessage();
callBack.responseData(responseData);
Log.d("www01", responseData);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//回调成功的数据
String responseData = request.body().toString();
callBack.responseData(responseData);
Log.d("www02", responseData);
}
});
//异步在V层接收
runOnUiThread(new Runnable() {
@Override
public void run() {
//子线程切换到UI线程中
Toast.makeText(LoginActivity.this, responseData, Toast.LENGTH_SHORT).show();
}
});
OkHttp 同步
try {
//携带用户名、密码的参数的URL
String request_url = Constant.LOGIN_URL + "?phone=" + userName + "&pwd=" + password;
//String request_url = Constant.BEAUTY_URL;
//OKHttp做登录处理
//创建OKHttpClient对象
OkHttpClient client = new OkHttpClient.Builder().build();
//表单请求体
FormBody formBody = new FormBody.Builder().build();
//创建Requst请求对象
Request request = new Request.Builder().method("POST", formBody).url(request_url).build();
//Request request = new Request.Builder().url(request_url).build();
//通过OKHttpClient对象调用newCall方法
Response response = client.newCall(request).execute();
//获取到Response,进行处理
String responseData = response.body().string();
//回显到P层
callback.responseData(responseData);
} catch (IOException e) {
e.printStackTrace();
}
//同步 M回传
//OKHttp的同步请求
Runnable runnable = new Runnable() {
@Override
public void run() {
requestLoginData(userName, password,callback);
}
};
//封装okhttp
public class OkUtil {
public static OkUtil okUtil;
private final OkHttpClient okHttpClient;
//私有构造函数
private OkUtil(){
okHttpClient = new OkHttpClient.Builder().build();
}
//DCL模式
public static OkUtil getInstance(){
if(null == okUtil){
synchronized (OkUtil.class){
if(null == okUtil){
return new OkUtil();
}
}
}
return okUtil;
}
//
public void get(String s, Callback callback){
Request request = new Request.Builder().url(s).build();
okHttpClient.newCall(request).enqueue(callback);
}
}
//封装的实现
OKHTTPUtil.getIns().get(UrlData.FL_URL, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d("ShopModel", "失败");
callBack.responseData(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("ShopModel", "成功");
callBack.responseData(response.body().toString());
}
});
//OkGo
OkGo.<String>get(UrlBean.BEAUTY_URL).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String s = response.body().toString();
//解析
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(s, JsonBean.class);
//回传值
callBack.requestData(jsonBean);
}
});
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/16249.html