接口:
@POST("interface/interface.ac")
Call<JsonObject> postData(@Body RequestBody data);
@GET("interface/interface.ac")
Call<String> postData1(@Query("data") String data);
返回json:
public static void postData(final Context context,String data) {
OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain)
throws IOException {
// TODO Auto-generated method stub
Request request = chain
.request()
.newBuilder()
.addHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Connection", "close")
.addHeader("Accept", "*/*").build();
return chain.proceed(request);
}
});
OkHttpClient client = httpBuilder
.readTimeout(Const.timeout, TimeUnit.MILLISECONDS)
.connectTimeout(Const.timeout, TimeUnit.MILLISECONDS)
.writeTimeout(Const.timeout, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(false).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(Const.UrlApi)
.client(client)
.addConverterFactory(GsonConverterFactory.create()).build();
HttpInterface httpInterface = retrofit.create(HttpInterface.class);
data="code="+fileCode+"&data="+data;
RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"),data);
Call<JsonObject> repos = httpInterface.postData(body);
repos.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> arg0,
Response<JsonObject> arg1) {
// TODO Auto-generated method stub
L.i(TAG, "postData:" + arg1.body().toString());
}
@Override
public void onFailure(Call<JsonObject> arg0, Throwable arg1) {
// TODO Auto-generated method stub
L.i(TAG, "postData==============error.");
}
});
}
返回string:
public static void postData1(final Context context) {
HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(
new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
L.i(TAG, "OkHttp3:" + message);
}
});
loggingInterceptor.setLevel(level);
OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain)
throws IOException {
// TODO Auto-generated method stub
Request request = chain
.request()
.newBuilder()
.addHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Connection", "close")
.addHeader("Accept", "*/*").build();
return chain.proceed(request);
}
}).addInterceptor(loggingInterceptor);
OkHttpClient client = httpBuilder
.readTimeout(Const.timeout, TimeUnit.MILLISECONDS)
.connectTimeout(Const.timeout, TimeUnit.MILLISECONDS)
.writeTimeout(Const.timeout, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(false).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(Const.UrlApi)
.client(client)
//返回值为Oservable<T>
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//返回值为json
.addConverterFactory(GsonConverterFactory.create())
//返回值为string
.addConverterFactory(ScalarsConverterFactory.create()).build();HttpInterface httpInterface = retrofit.create(HttpInterface.class);Call<String> repos = httpInterface.postData1(data);repos.enqueue(new Callback<String>() {@Overridepublic void onFailure(Call<String> arg0, Throwable arg1) {// TODO Auto-generated method stubL.i(TAG, "postData1==============error.");}@Overridepublic void onResponse(Call<String> arg0, Response<String> arg1) {// TODO Auto-generated method stubL.i(TAG, "postData1:"+ arg1.body().toString());}});}
点击下载jar包