Retrofit2+okhttp3获取网络数据

接口:

@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包


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值