Tag: okhttp3

使用okHttp上传图片

我想要使​​用okhttp上传图片,但我无法find后期Image.What MultipartBuilder,而不是这个。 这是我的代码 public static JSONObject uploadImage(File file) { try { final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); RequestBody req = new MultipartBuilder().setType(MultipartBody.FORM).addFormDataPart("userid", "8457851245") .addFormDataPart("userfile","profile.png", RequestBody.create(MEDIA_TYPE_PNG, file)).build(); Request request = new Request.Builder() .url("url") .post(req) .build(); OkHttpClient client = new OkHttpClient(); Response response = client.newCall(request).execute(); Log.d("response", "uploadImage:"+response.body().string()); return new JSONObject(response.body().string()); } catch (UnknownHostException | UnsupportedEncodingException e) { Log.e(TAG, […]

使用OkHttp 3自动处理cookie

我正在使用okhttp 3.0.1。 每一个我得到的例子是与okhttp2的cookie处理 OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); 可以请一些人指导我如何在版本3中使用。setCookieHandler方法是不存在的版本3。

logging与改造2

我正在尝试获取请求中正在发送的确切的JSON。 这是我的代码: OkHttpClient client = new OkHttpClient(); client.interceptors().add(new Interceptor(){ @Override public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { Request request = chain.request(); Log.e(String.format("\nrequest:\n%s\nheaders:\n%s", request.body().toString(), request.headers())); com.squareup.okhttp.Response response = chain.proceed(request); return response; } }); Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_URL) .addConverterFactory(GsonConverterFactory.create()) .client(client).build(); 但是我只在日志中看到这个: request: com.squareup.okhttp.RequestBody$1@3ff4074d headers: Content-Type: application/vnd.ll.event.list+json 我该如何做适当的日志logging,因为删除了我们用于Retrofit 1的setLog()和setLogLevel() ?