retrofit2

Upload picture to server using retrofit 2

女生的网名这么多〃 提交于 2019-12-12 04:03:59
问题 First of all lets look to the code: public interface ApiInterface { @Multipart @POST("my/files/photo/") Call<FileUploadResponse> uploadPhoto(@Header("authorization") String auth, @Part MultipartBody.Part file, @Part("file-type") RequestBody fileType); } I'm calling this interface like this: RequestBody body = RequestBody.create(MediaType .parse(getContentResolver().getType(fileUri)), file); MultipartBody.Part avatarBody = MultipartBody.Part .createFormData(data, file.getName(), body);

How to deserialize json with a string-url name-value pair

南笙酒味 提交于 2019-12-12 03:46:46
问题 I have json response in the form { "total": 782, "category": { "id": 1120, "name": "Computers & Programming" }, "experts": [ { "id": 385816, "name": "Master Z", "title": "Mr", "description": " Expert in C++", "profile": "http://...com/experts.svc/1.0/385816/..../json?.." } } ] } I am able to parse everything else in the experts[] array except "profile" whose value "http://...com/experts.svc/1.0/385816/..../json?.." has the following json response { "id": 385816, "name": "Master Z", "title": "

Deserialze json with a wrapper keyword with Gson

六月ゝ 毕业季﹏ 提交于 2019-12-12 03:28:52
问题 I am using retroft and gson for request/response in my app. The json structure I get from server for json object is like: { "data": { "name": "Rogelio Volkman", "address": "27299 Will Bridge Suite 058\nWest Reubenhaven, MI 00736", "lat": 54.65, "lng": 111.75, "phone": "+26(4)5015498663", "user": { "data": [ { "name": "Mehrdad" } ] } } } As you see every model is wrapped around data keyword. For json array response the result is like: { "data": [ { "name": "Rogelio Volkman", "address": "27299

Sending image file with retrofit 2

非 Y 不嫁゛ 提交于 2019-12-12 03:06:56
问题 I am trying to upload an image to our project's server using Retrofit 2 The image is picked through an image picking activity and seems to work since the file (image) can be displayed using Picasso. Retrofit succeeds however the server doesn't seem to get the file. Here is the server side part. func (c *gin.Context) { file, header , err := c.Request.FormFile("profileImage") // err = http: no such file } Sever side error message Even the RequestBody prints coherent information when I tested it

OkHttp with SNI: Connection reset on Java 7

徘徊边缘 提交于 2019-12-12 02:52:45
问题 The following snippet works perfectly as long as only one tls certificate is enabled on the nginx server. OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("ENDPOINT").build(); Response response = client.newCall(request).execute(); System.out.println(response.body().string()); Activating a site with a different certificate causes nginx to enable SNI. That apparently breaks both on Oracle Java 7 as well as OpenJDK 7. Exception in thread "main" java.net

Complex JSON with generics using Retrofit2

╄→гoц情女王★ 提交于 2019-12-12 02:46:56
问题 In my application, most of the JSON that I receive from the server have the following structure: { "IsError":false, "Result":{ "key1":"value", "key2":"value", ... } } I've followed @fernandospr 's suggestion in my original question about GSON and tried using generics to avoid writing two objects (Response and Result) per JSON and having a generic Response Object which would adapt to any kind of Result; as: public class GenericResponse<T> { @SerializedName("IsError") private boolean isError;

Image file not going to server using retrofit 2 post method

谁说胖子不能爱 提交于 2019-12-12 02:27:18
问题 Hi friends please help me out here i am new for the retrofit i am trying use the post method in the retrofit to send the image file by converting it into base64 format but it is not uploading please friends help me out this is my code Retrofit code Retrofit registerretrofit = new Retrofit.Builder().baseUrl(Constant.url).client(Constant.okClient()).addConverterFactory(GsonConverterFactory.create()).build(); Api registerapi = registerretrofit.create(Api.class); String urlbitmap=Constant

Realm+Gson+Retrofit2 parsing

六月ゝ 毕业季﹏ 提交于 2019-12-12 02:12:06
问题 Retrofit2 response *@Override public void onResponse(Call<List<Category>> call, final Response<List<Category>> response) { switch (response.code()) { case 200: if (response.body() != null && !response.body().isEmpty()) { final List<Category> categoryList = response.body(); Realm.getDefaultInstance().executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { for (Category category : categoryList) { realm.insertOrUpdate(category); } } }); } RealmObjects

How to handle null param values in Retrofit

有些话、适合烂在心里 提交于 2019-12-12 01:38:16
问题 We're moving from Apache's http client to Retrofit and we've found some edge cases where param values can be null. Apache used to intercept these and turn them into empty strings, but Retrofit throws an IllegalArgumentException. We want to replicate the old behavior so that it doesn't cause any unexpected issues out in production. Is there a way for me to swap these null values with empty strings before ParameterHandler throws an exception? 回答1: You can try the following: My web service (Asp

Retrofit2 put token in URL with placeholder

这一生的挚爱 提交于 2019-12-12 00:51:21
问题 I have a Problem with replace the placeholder in the url with the auth-token. My OkHttpClient: OkHttpClient clientToken = new OkHttpClient.Builder() .addInterceptor(tokenInterceptor) .addInterceptor(interceptor) .connectTimeout(100, TimeUnit.SECONDS) .readTimeout(100, TimeUnit.SECONDS) .build(); My TockenInspector: private class TokenInterceptor implements Interceptor{ @Override public Response intercept(Chain chain) throws IOException { Response mainResponse = chain.proceed(chain.request());