Parse JSON array response using Retrofit & Gson

Please try to use this one @FormUrlEncoded @POST(“api/sponsors”) Call<List<SponsorsResult>> getStatesAndDistrict( @Field(“xyz”) String field1 ); Call <List<SponsorsResult>> call = service.getSponsorsValue(); call.enqueue(new Callback<List<SponsorsResult>>() { @Override public void onResponse(Call<List<SponsorsResult>> call, Response<List<SponsorsResult>> response) { List<SponsorsResult> rs = response.body(); } @Override public void onFailure(Call<List<SponsorsResult>> call, Throwable t) { } }); class SponsorsResult { @SerializedName(“sponsors”) private List<SponsorsValue> sponsors; public List<SponsorsValue> getSponsors() … Read more

Retrofit-2 Content-Type Issue

Try this type header for Retrofit 1.9 and 2.0. For Json Content Type. @Headers({“Accept: application/json”}) @POST(“user/classes”) Call<playlist> addToPlaylist(@Body PlaylistParm parm); You can add many more headers i.e @Headers({ “Accept: application/json”, “User-Agent: Your-App-Name”, “Cache-Control: max-age=640000” }) Dynamically Add to headers: @POST(“user/classes”) Call<ResponseModel> addToPlaylist(@Header(“Content-Type”) String content_type, @Body RequestModel req); Call you method i.e mAPI.addToPlayList(“application/json”, playListParam); Or Want … Read more

How can I debug my retrofit API call?

Use HttpLoggingInterceptor along with Retrofit. If this helps, add this inside your build.gradle – //Retrofit and OkHttp for Networking implementation ‘com.squareup.retrofit2:retrofit:2.9.0’ implementation ‘com.squareup.retrofit2:converter-gson:2.9.0’ //Logging Network Calls implementation ‘com.squareup.okhttp3:logging-interceptor:4.9.1’ Inside your APIClient class add this – public class ApiClient { private static Retrofit retrofit = null; public static Retrofit getClient(){ HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); … Read more

Retrofit Uploading multiple images to a single key

We can use MultipartBody.Part array to upload an array of images to a single key. Here is the solution WebServicesAPI @Multipart @POST(WebServices.UPLOAD_SURVEY) Call<UploadSurveyResponseModel> uploadSurvey(@Part MultipartBody.Part[] surveyImage, @Part MultipartBody.Part propertyImage, @Part(“DRA”) RequestBody dra); Here is the method for uploading the files. private void requestUploadSurvey () { File propertyImageFile = new File(surveyModel.getPropertyImagePath()); RequestBody propertyImage = RequestBody.create(MediaType.parse(“image/*”), propertyImageFile); … Read more

Retrofit Post Parameter

Try using this public interface SafeUserApi { @FormUrlEncoded @POST(“/api/userlogin”) void getUserLogin( @Field(“client_id”) String id, @Field(“client_secret”) String secret, @Field(“username”) String uname, @Field(“password”) String password, Callback<LoginResult> cb ); } Here parm1 is the POST parameter that you will be passing it to the server. This will solve your problem in case if you are using PHP u … Read more

How can I return String or JSONObject from asynchronous callback using Retrofit?

I figured it out. It’s embarrassing but it was very simple… Temporary solution may be like this: public void success(Response response, Response ignored) { TypedInput body = response.getBody(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(body.in())); StringBuilder out = new StringBuilder(); String newLine = System.getProperty(“line.separator”); String line; while ((line = reader.readLine()) != null) { out.append(line); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)