package com.hdnetworklib.network.http;
import java.util.Map;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
/**
* Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
*/
public interface RetrofitServiceApi {
@POST
Call<ResponseBody> reqPost(@Url String url, @Body RequestBody requestBody);
@GET
Call<ResponseBody> reqGet(@Url String url, @QueryMap Map<String, String> options);
@GET
Call<ResponseBody> reqGet(@Url String url);
}
package com.hdnetworklib.network.http;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by wangyuhang@evergrande.cn on 2017/7/12 0012.
*/
public class HttpClient {
private static final String TAG = "HttpClient";
private static volatile HttpClient instance;
private HttpClient() {
}
public static HttpClient getInstance() {
if (instance == null) {
synchronized (HttpClient.class) {
if (instance == null) {
instance = new HttpClient();
}
}
}
return instance;
}
/**
* Http Post请求
*
* @param req_id 请求编号
* @param method 请求业务方法
* @param url 请求的URL
* @param jsonData POST需要所带参数(JSON串格式)
* @param callback 回调接口
*/
public void reqPostHttp(final int req_id, final String method, String url, String jsonData, final HttpCallback callback) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.what.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitServiceApi retrofitServiceApi = retrofit.create(RetrofitServiceApi.class);
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonData);
Call<ResponseBody> call = retrofitServiceApi.reqPost(url, body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String result = response.body().string();
Log.i(TAG, "reqPostHttp onResponse: " + result);
if (callback != null) {
callback.onSuccess(new HttpResMsg(req_id, method, result));
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "reqPostHttp onResponse exception: " + e.toString());
if (callback != null) {
callback.onError(e.toString());
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "reqPostHttp onFailure: " + t.toString());
if (callback != null) {
callback.onError(t.toString());
}
}
});
}
/**
* Http Get请求
*
* @param req_id 请求编号
* @param method 请求业务方法
* @param url 请求的URL
* @param options GET需要所带参数键值(如果URL里带有则不需要在此添加)
* @param callback 回调接口
*/
public void reqGetHttp(final int req_id, final String method, String url,
Map<String, String> options, final HttpCallback callback) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.what.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitServiceApi retrofitServiceApi = retrofit.create(RetrofitServiceApi.class);
Call<ResponseBody> call = null;
if (options == null) {
call = retrofitServiceApi.reqGet(url);
} else {
call = retrofitServiceApi.reqGet(url, options);
}
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String result = response.body().string();
Log.i(TAG, "reqPostHttp onResponse: " + result);
if (callback != null) {
callback.onSuccess(new HttpResMsg(req_id, method, result));
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "reqPostHttp onResponse exception: " + e.toString());
if (callback != null) {
callback.onError(e.toString());
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "reqPostHttp onFailure: " + t.toString());
if (callback != null) {
callback.onError(t.toString());
}
}
});
}
}
baseUrl([url=http://www.what.com/]http://www.what.com/[/url])
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1")
.addConverterFactory(GsonConverterFactory.create())
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://112.124.22.238:8081/course_api/wares/hot?pageSize=1&curPage=1/")
.addConverterFactory(GsonConverterFactory.create())
.build();
package com.hdnetworklib.network.http;
/**
* Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
*/
public interface HttpCallback {
void onSuccess(HttpResMsg httpResMsg);
void onError(String errorMsg);
}
package com.hdnetworklib.network.http;
/**
* Created by wangyuhang@evergrande.cn on 2017/8/23 0023.
*/
public class HttpResMsg {
private Integer req_id;
private String method;
private String data;
public HttpResMsg() {
}
public HttpResMsg(int req_id, String method, String data) {
this.req_id = req_id;
this.method = method;
this.data = data;
}
public Integer getReq_id() {
return req_id;
}
public void setReq_id(Integer req_id) {
this.req_id = req_id;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有