retrofit2

Unable to parse error in Retrofit 2

喜你入骨 提交于 2019-12-22 05:26:07
问题 I am using Retrofit 2 and I am getting a null pointer exception at this line: RetrofitClient.APIError error = RetrofitClient.ErrorUtils.parseError(response, retrofit); The error is null. More details: This is the format that the API returns the error in: { "error": { "message": "Incorrect credentials", "statusCode": 401 } } Here is my login Callback code: new Callback<LoginResponse>() { @Override public void onResponse(Response<LoginResponse> response, Retrofit retrofit) { if (listener !=

setLevel okhttp LoggingInterceptor deprecated

∥☆過路亽.° 提交于 2019-12-22 03:52:23
问题 setLevel(okhttp3.logging.HttpLoggingInterceptor.Level)' is deprecated what should replace with setLevel? to remove the deprecated issue 回答1: According to the documentation "Moved to var. Replace setLevel(...) with level(...) to fix Java", Replace setLevel(...) with level(...) will fix this issue example : HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.level(HttpLoggingInterceptor.Level.BODY); Happy coding :) 回答2: for Kotlin replace : val logger: HttpLoggingInterceptor

Retrofit — Multiple query parameters of same name where name is set dynamically

 ̄綄美尐妖づ 提交于 2019-12-21 21:43:13
问题 I'm trying to migrate an old project to Retrofit library and this project has quite tricky API. So I have a query template like this: @GET(value = "products/search") Single<ProductSearchResponse> productSearch(); And I have to add some parameters here of following template: filter[attributeId]=attributeValueId For example: products/search?filter[1]=10&filter[1]=11&filter[2]=20&filter[2]=21 That's how API works and I can't change it. I know that we can pass a list as a parameter, like this:

how to use retrofit 2 to send file and other params together

无人久伴 提交于 2019-12-21 20:17:24
问题 I am looking for an example how could I send file and other params together to server. I have to send server JSON which { "title": "title", "file": "uploaded file instance", "location": { "lat": 48.8583, "lng": 2.29232, "place": "Eiffel Tower" } } How could I create Retrofit to handle this case? If file is a string I know how to handle this. If file is File object I have no idea how to do this. 回答1: Use gson and create a model class for the location. Add the following dependencies to your

Send authorization header with every request in webview using okhttp in android

眉间皱痕 提交于 2019-12-21 17:07:14
问题 I am using WebView to display web page, but the server expects an authorization token with every request from my webview. Anybody know if this is possible ? I referred this post in #SO adding-header-to-all-request-with-retrofit-2. But I'm not able to get the result. This is my code (bare with my coding standard, I'm a beginner) public class TableViewTest extends AppCompatActivity { ScrollView scrollView; WebView webView; SharedPreferences pref; @SuppressLint("SetJavaScriptEnabled") @Override

How to handle response which can be different Type in Retrofit 2

陌路散爱 提交于 2019-12-21 05:49:05
问题 In WebApi returned JSON's field can be of different class: { someField:"some string" } { someField: { "en" : "some string", "ka" : "რამე სტრინგი" } } I've seen some solutions, but it was on previous versions of Retrofit. How would my pojo class look like and what can i use to parse this dynamic json? 回答1: For your case you can use Call<JsonElement> as response type and parse it in response: call.enqueue(new Callback<JsonElement>() { @Override public void onResponse(Call<JsonElement> call,

Retrofit add header with token and id

Deadly 提交于 2019-12-21 05:12:17
问题 I have a problem with getting authenticated user. Before it I got token and user id. Now i need to get user from server using access token and id. I have header format Now I'am trying to add header with user token and id using interceptor. My code: Interceptor interceptor = new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("Accept", "application/json") .addHeader("authorization", token

Retrofit parse JSON dynamic keys

我的梦境 提交于 2019-12-21 05:02:20
问题 I'm a newbie in Retrofit. How to parse the Json below using retrofit? { "data": { "Aatrox": { "id": 266, "title": "a Espada Darkin", "name": "Aatrox", "key": "Aatrox" }, "Thresh": { "id": 412, "title": "o Guardião das Correntes", "name": "Thresh", "key": "Thresh" } }, "type":"champion", "version":"6.23.1" } 回答1: You could make your model POJO contain a Map<String, Champion> to deserialize into, to deal with the dynamic keys. Example: public class ChampionData { public Map<String, Champion>

Retrofit JSON deserializing object's $ref reference to its original copy

梦想与她 提交于 2019-12-21 04:53:11
问题 I am using Microsoft.Net with Breeze for APIs and the results I get using Retrofit have nested repeated same objects. For example EmployeeJob has Customer navigation property so the APIs result looks like this { Id:1, "Customer_Id": 39, "Customer": { "$id": "2", "$type": "Wit.Trade.Entities.Customer, Wit.Trade", "CourtesyTitle": "Mr", "FirstName": "Ahmad" } } { Id:2 "Customer_Id": 39, "Customer": { "$ref": "2" //here same customer Ahmad }, } Now the Java List I get of these EmployeeJobs has

How to return value using Async Retrofit 2.0

末鹿安然 提交于 2019-12-21 04:26:08
问题 I am new with retrofit, i have a function with Async Retrofit, with purpose like this example public boolean bookmark(){ boolean result = false; Call<Response> call = service.bookmark(token, request); call.enqueue(new Callback<Response>() { @Override public void onResponse(Call<Response> call, retrofit2.Response<Response> response) { result = true; } @Override public void onFailure(Call<Response call, Throwable t) { } }); return result; } but i dont know how to return that value. 回答1: You can