retrofit2

Method invocation may produce NullPointerException Retrofit Body

微笑、不失礼 提交于 2020-02-03 03:59:05
问题 I am using Retrofit 2 for get response from my API and store its value in my constant like below if(response.isSuccessful()) { constant.banner_on = response.body().getBanner_on(); constant.int_on = response.body().getInt_on(); constant.int_click = response.body().getInt_click(); } its giving me warning on all three like below Method invocation getBanner_on may produce java.lang.nullPointerException I am confused and unable to resolve this warning. Let me know if someone can help me to come

Method invocation may produce NullPointerException Retrofit Body

只愿长相守 提交于 2020-02-03 03:58:50
问题 I am using Retrofit 2 for get response from my API and store its value in my constant like below if(response.isSuccessful()) { constant.banner_on = response.body().getBanner_on(); constant.int_on = response.body().getInt_on(); constant.int_click = response.body().getInt_click(); } its giving me warning on all three like below Method invocation getBanner_on may produce java.lang.nullPointerException I am confused and unable to resolve this warning. Let me know if someone can help me to come

Service Generator Retrofit

混江龙づ霸主 提交于 2020-01-31 20:08:26
问题 Can someone help me understand the createService method in below code. I need to understand what is method's parameter Class S and the code underneath in depth public class ServiceGenerator { public static final String API_BASE_URL = Constant.BASE_URL; private static OkHttpClient httpClient = new OkHttpClient(); private static Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()); public static <S> S createService(Class<S>

How do I get “nameValuePairs” in a JSON request using Retrofit?

安稳与你 提交于 2020-01-29 09:23:06
问题 How do I post post a JSONObject request like below? Original REQUEST: { "pObj": [], "robj": [ { "l_index": "1", "user_id": "111", "vername": "1", "fcmtoken": "ghkghkhkh" } ], "uobject": [], "pname": "y6y68uuy7" } In Volley I can successfully post JSONObject request. But when I am using this in Retrofit my request is changed as below: What I am get in log: { "nameValuePairs": { "pObj": { "values": [] }, "robj": { "values": [ { "nameValuePairs": { "l_index": "1", "user_id": "111", "vername": "1

OkHttpClient throws exception after upgrading to OkHttp3

本小妞迷上赌 提交于 2020-01-28 17:40:23
问题 I'm using following lines of code to add a default header to all of my requests sent using Retrofit2: private static OkHttpClient defaultHttpClient = new OkHttpClient(); static { defaultHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON").build(); return chain.proceed(request); } }); } After upgrading retrofit to beta-3 version, I

OkHttpClient throws exception after upgrading to OkHttp3

人盡茶涼 提交于 2020-01-28 17:40:15
问题 I'm using following lines of code to add a default header to all of my requests sent using Retrofit2: private static OkHttpClient defaultHttpClient = new OkHttpClient(); static { defaultHttpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .addHeader("Accept", "Application/JSON").build(); return chain.proceed(request); } }); } After upgrading retrofit to beta-3 version, I

JSON to POJO with nested values

纵饮孤独 提交于 2020-01-25 07:30:31
问题 So I have such json response: "data": [ { "id": 11881, "date": "2018-03-26T16:22:08", "date_gmt": "2018-03-26T14:22:08", "guid": { "rendered": "http://google.com" }, "modified": "2018-03-26T16:22:08", "modified_gmt": "2018-03-26T14:22:08", "slug": "some text", "status": "some status", "type": "post", "link": "http://google.com", "title": { "rendered": "some title" } } ] How can I parse such response to POJO class if I would have such classes: public class Response{ private List<Post> data; }

Getting response code 200 in Postman but not on Android Network library

三世轮回 提交于 2020-01-24 13:07:45
问题 I have a POST method API which is giving 200 response code in Postman but not when calling api by using Volley or even in Retrofit2 . Here is Postman screenshots: Here what i did for Volley library code: final String url = "http://xxxxxxxx.com/api/mobile/user/post/"; StringRequest stringReq = new StringRequest(Request.Method.POST, url, new com.android.volley.Response.Listener<String>() { @Override public void onResponse(String response) { Log.e("onResponse ===", response + " " ); } }, new com

Retrofit 2.x : Log Header for request and response

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-24 02:26:04
问题 I am using retrofit 2.x and i want to log the header and body of request and response . HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .addNetworkInterceptor(new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { Request request = chain.request()

Retrofit - Expected BEGIN_ARRAY but was BEGIN_OBJECT?

霸气de小男生 提交于 2020-01-24 00:46:49
问题 I am getting a json result from service with retrofit like bellow : { "result": { "totalCount": 15, "resultCount": 2, "offset": 0, "limit": 2, "products": [ { "id": 10081, "name": "prod", "pictureUrl": "url", "price": 1, "url": "url", "briefDescription": "test", "description": "test", "pictures": [], "categoryTitle": "s s", "categoryId": 53, "extraInfo": { "productProperties": [ { "id": 88, "value": "6", "measurementUnit": "s", "title": "s" }, { "id": 89, "value": "2", "measurementUnit": "s",