Getting 403 status code in retrofit2 in POST request - java

I am trying to post JSON data in the server using retrofit2 but I am getting 403 status code, see my postman window,
i tried passing all headers that is added by postman but still it gives 403 status code, and it is running successfully in postman but not running in android retrofit
Hear
my retrofit codes are :-
public interface ApiInterface {
String BASE_URL = "https:example.com";
#Headers({"Content-Type: application/json",
"User-Agent: PostmanRuntime/7.29.2",
"Accept: */*",
"Cache-Control: no-cache",
"Postman-Token: 9151e8a4-7665 - 416d - b2f5 - a84f0736a7ba",
"Host: xyz.com",
"Accept-Encoding:gzip, deflate, br",
"Content-Length: 76" })
#POST("add_aadharinfo")
Call<DataClass> posImages(#Body JSONObject data);
}
My MainActivity retrofit codes:-
private void ImageInfo() throws JSONException {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiInterface.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
JSONObject dataInfo = new JSONObject();
dataInfo.put("phone",contact);
dataInfo.put("profile_image",encoded);
dataInfo.put("aadhar_image",encoded22);
Call<DataClass> usercall = apiInterface.posImages(dataInfo);
usercall.enqueue(new Callback<DataClass>() {
#Override
public void onResponse(Call<DataClass> call, retrofit2.Response<DataClass> response) {
if (response.isSuccessful())
{
Toast.makeText(SignUpActivity.this,response.body().getMessage(),Toast.LENGTH_LONG).show();
}
Log.d("response", "onResponse: "+response.code());
}
#Override
public void onFailure(Call<DataClass> call, Throwable t) {
Toast.makeText(SignUpActivity.this,"fail"+t.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
});
}
My dataClass:-
public class DataClass {
private String message;
private String data;
public DataClass(String message, String data) {
this.message = message;
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}

Related

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message

i am making login function in android using retrofit. I have created an endpoint for login validation, then I have tested it using Postman using raw (json) and it worked. But when I enter the endpoint into android using retrofit I get an error message like this:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message
can anyone help me?
So here my source:
ApiClient
public class ApiClient {
public static final String BASE_URL = "";
public static Retrofit retrofit;
public static Retrofit getRetrofit() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
AuthInterface
public interface AuthInterface {
#Headers("Content-Type: application/json")
#POST("auth/login")
Call<AuthPost> authPostCall(#Body String body);
}
AuthPost
public class AuthPost {
#SerializedName("status")
private String status;
#SerializedName("error_code")
private int error_code;
#SerializedName("message")
private String message;
#SerializedName("token")
private String token;
...getter and setter
}
LoginActivity
JSONObject payload = new JSONObject();
try {
payload.put("login_username", loginUsernameText);
payload.put("login_password", loginPasswordText);
} catch (JSONException e) {
e.printStackTrace();
}
Call<AuthPost> authPostCall = authInterface.authPostCall(payload.toString());
authPostCall.enqueue(new Callback<AuthPost>() {
#Override
public void onResponse(Call<AuthPost> call, Response<AuthPost> response) {
if (response.code() == 200) {
} else {
}
}
#Override
public void onFailure(Call<AuthPost> call, Throwable t) {
t.printStackTrace();
}
});
Are you sure about:
#SerializedName("message")
private String message;
Usually this error appears if this field is Object.
Does your JSON looks like
"message":"test"
or something like:
"message":{"field":"value"}
If it is the second variant so you should simple change the field to necessary type.

Convert Java object into JSON array for Retrofit call

I need to send an array of Strings using a Retrofit call. To do that I decided to create an object like this one:
public class SendEmailsList {
ArrayList<String> emails;
public SendEmailsList(ArrayList<String> emails) {
this.emails = emails;
}
}
And my JSON String must be like this:
{
"emails": ["email#server.com","email1#server.com","email2#server.com"]
}
This is the POST method defined in my interface:
#POST("/v2/companies/{companyId}/invite")
Call<ArrayList<String>> inviteMembers(#Path("companyId") String companyId, #Body SendEmailsList emails);
And this is the method that makes the Retrofit call:
public void SendNetworkRequest() {
OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
okhttpBuilder.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder newRequest = request.newBuilder().header("Authorization", "Bearer " + token);
return chain.proceed(newRequest.build());
}
});
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BuildConfig.ENDPOINT)
.client(okhttpBuilder.build())
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
CompanyService invite = retrofit.create(CompanyService.class);
Call<ArrayList<String>> call = invite.inviteMembers("5602eb7ce49c9cd70409f206", new SendEmailsList(invitedEmails));
call.enqueue(new Callback<ArrayList<String>>() {
#Override
public void onResponse(Call<ArrayList<String>> call, Response<ArrayList<String>> response) {
System.out.println("Internal Users: " + response.code());
}
#Override
public void onFailure(Call<ArrayList<String>> call, Throwable t) {
// Log error here since request failed
Log.e("Internal Users Activity", t.toString());
}
});
}
But I am getting this error:
12-21 14:36:49.953 27953-27953/com.construct.test E/Internal Users Activity: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
How can I figure out what is going on?

Handling Retrofit status codes without a pojo class

I got the following response from my server: status code 201 Created.
There is no actual response (returned object, etc.), so there is not need to create a POJO class.
So, I don't know how I should handle this status code without creating a POJO class. Is there any option to make write the code without using a POJO class?
Retrofit API has Response class that can encapsulate your response.
As long as you don't want to bother with the response data you can implement your service as:
interface CustomService {
#GET("whatever")
Call<Response<Void>> getAll();
// Or using RxJava:
#GET("whatever")
Single<Response<Void>> getRxAll();
}
Then implement your callback:
private Callback<Response<Void>> responseHandler = new Callback<Response<Void>>() {
#Override
public void onResponse(Call<Response<Void>> call, Response<Response<Void>> response) {
final int code = response.code();
// TODO: Do whatever you want with the response code.
}
#Override
public void onFailure(Call<Response<Void>> call, Throwable t) {
// TODO: Handle failure.
}
}
Or reactive consumer:
private Consumer<Response<Void>> responseRxHandler = new Consumer<Response<Void>>() {
#Override
public void accept(Response<Void> response) throws Exception {
final int responseCode = response.code();
// TODO: Do whatever you want with the response code.
}
};
Debugging result:
You can try the following code.
Can get the response without a POJO class by getting using ResponseBody format and then you can parse it normally like ordinary JSON parsing.
Api Call:
Call<ResponseBody> call = service.callLogin(AppConstants.mApiKey, model_obj);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.code() == 201)
{
JSONObject jobjresponse = null;
try {
jobjresponse = new JSONObject(mResponse.body().string());
String status = jobjresponse.getString("status");
JSONObject result = jobjresponse.getJSONObject("results");
String msg = result.getString(“msg”);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
Retrofit Interface class:
public interface RetrofitInterface {
#Headers({"Content-Type: application/json", "Cache-Control: max-age=640000"})
#POST("v1/auth/")
public Call<ResponseBody> callLogin(#Query("key") String key, #Body LoginModel body);
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(“base url”)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
Sample Response:
{ "status":"true", "result":{"msg”:”created successfully”} }

android retrofit2 could not add header (415 error code)

I'm trying to access ticket data via skyscanner api and pass it to my view, but I cannot accomplish that, because I get 415 error code I'm using retrofit2 and adding header programmatically. My interface looks like this:
public interface GetFlightDetails {
#POST("apiservices/pricing/v1.0/")
Call<TicketData> getFlightList(#Query("apiKey") String apiKey,
#Query("country") String country,
#Query("currency") String currency,
#Query("locale") String locale,
#Query("originPlace") String originPlace,
#Query("destinationPlace") String destinationPlace,
#Query("outboundPartialDate")String outboundPartialDate,
#Query("inboundPartialDate") String inboundPartialDate,
#Query("locationschema") String locationschema,
#Query("cabinclass") String cabinclass,
#Query("adults") int adults,
#Query("children") int children,
#Query("infants") int infants,
#Query("groupPricing") boolean groupPricing) ;
}
and in my activity, when I'm ready to make a request I have the following code:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
//adding logging
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClient.interceptors().add(logging);
//headers
httpClient.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request original = chain.request();
//adding header info
Request request = original.newBuilder()
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();
GetFlightDetails api = retrofit.create(GetFlightDetails.class);
Call<TicketData> mresponse = api
.getFlightList(API_KEY, country, currency, locale, from, to,
departDate.substring(0,10), returnDate.substring(0,10),
locationSchema, cabinClass, adult, children, infants, false);
mresponse.enqueue(new Callback<TicketData>()
{
#Override
public void onResponse(Call<TicketData> call, Response <TicketData> response) {
if (!response.isSuccessful()){
Log.d("UnSuccess", response.raw().toString());
return;
}
else {
progress.cancel(); //cancel progress dialog
Log.d("Success", response.raw().toString());
TicketData ticketData = response.body();
RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), ticketData);
mRecyclerView.setAdapter(adapter);
}
}
#Override
public void onFailure(Call<TicketData> call, Throwable t){
progress.setMessage("Retrofit Error Occured");
}
});
and in my log file I see the following error:
com.example.ex D/OkHttp: --> POST http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US&currency=USD&locale=en-us&originPlace=SFO&destinationPlace=LAX&outboundPartialDate=2016-10-24&inboundPartialDate=2016-10-31&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false http/1.1 (0-byte body)
com.example.ex D/OkHttp: <-- 415 Unsupported Media Type http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US&currency=USD&locale=en-us&originPlace=SFO&destinationPlace=LAX&outboundPartialDate=2016-10-24&inboundPartialDate=2016-10-31&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false (403ms, 0-byte body)
com.example.ex D/UnSuccess: Response{protocol=http/1.1, code=415, message=Unsupported Media Type, url=http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US&currency=USD&locale=en-us&originPlace=SFO&destinationPlace=LAX&outboundPartialDate=2016-10-24&inboundPartialDate=2016-10-31&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false}
I'm not sure why it occurs, because I've tried to add headers in my interface.
Skyscanner docs reference 1 and reference 2
Thanks!
Please refer same type of example : Check your parameter type which filed is query string and which field is #Field and etc.,
public static final String TRAVEL_API = "http://business.skyscanner.net/";
public interface TravelApiInterface {
#FormUrlEncoded
#Headers("Content-Type:application/x-www-form-urlencoded; charset=UTF-8")
#POST("/apiservices/pricing/v1.0/")
void getTravelApi(
#Field("country") String country,
#Field("currency") String currency,
#Field("locale") String locale,
#Field("locationSchema") String locationSchema,
#Field("apikey") String apikey,
#Field("grouppricing") String grouppricing,
#Field("originplace") String originplace,
#Field("destinationplace") String destinationplace,
#Field("outbounddate") String outbounddate,
#Field("inbounddate") String inbounddate,
#Field("adults") int adults,
#Field("children") int children,
#Field("infants") int infants,
#Field("cabinclass") String cabinclass, Callback<Object> response);
}
TravelApiInterface currencyRequestInterfaceService;
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Url.TRAVEL_API)
.setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade request) {
request.addQueryParam(getString(R.string.api_key_title), getString(R.string.api_key_value));
}
})
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
currencyRequestInterfaceService = adapter.create(TravelApiInterface.class);
====================
sample request
requestConversion("UK", "GBP", "en-GB", "iata", "xxxxxxxx","on", "EDI", "LHR",
"2016-10-04", "2016-10-11", 1, 0, 0, "Economy", new Callback<Object>() {
#Override
public void success(Object o, Response response) {
}
#Override
public void failure(RetrofitError error) {
}
});
In strings.xml
<string name="api_key_title">apikey</string>
<string name="api_key_value">xxxxxxx</string>
We need to give Content-Type for this problem and need to pass as model class
#Headers("Content-Type:application/json")
#POST("saveAddressByFE")
Call<ChangeAddressModel> updateAddress(#Body AddressModel addressModel );
Retrofit response
private void getUpdateAddress(AddressModel addressModel) {
UploadService service = APIClient.getClient(CommonSettings.MY_RECORDING_SERVER_URL).create(UploadService.class);
Call<ChangeAddressModel> call = service.updateAddress(addressModel);
call.enqueue(new Callback<ChangeAddressModel>() {
#Override
public void onResponse(Call<ChangeAddressModel> call, Response<ChangeAddressModel> response) {
if (response.body().getMessage().equalsIgnoreCase("success")) {
Toast.makeText(InsuranceAgentDetailsActivity.this, "Submitted Successfully", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<ChangeAddressModel> call, Throwable t) {
t.printStackTrace();
Toast.makeText(InsuranceAgentDetailsActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
}
});
}
Model class
public class ChangeAddressModel {
#SerializedName("message")
#Expose
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
the data we are going to send model class(AddressModel addressModel)
public class AddressModel {
private String addressType,address,custId;
public AddressModel(String addressType, String address, String custId) {
this.addressType = addressType;
this.address = address;
this.custId = custId;
}
public String getAddressType() {
return addressType;
}
public void setAddressType(String addressType) {
this.addressType = addressType;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCustId() {
return custId;
}
public void setCustId(String custId) {
this.custId = custId;
}
}

Make a GET and POST service call with Retrofit with the use of Protobuf (Protocol Buffer)

Can anyone please give me some example how we can use protobuf in retrofit - I tried but its failed with some error , let me give you a sample of my implementation on that.
I hope you guys will help me.
ApiInterface.java
public interface ApiInterface {
#GET
Call<CommonProto.Country> makeGetRequest(#Url String url);
}
ApiClient.java
public class ApiClient {
public static final String BASE_URL = "**************************";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Proto3ConverterFactory.create())
.build();
}
return retrofit;
}
}
MainActivity.java
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<CommonProto.Country> call = apiService.makeGetRequest("Services/CountryServices/GetAllCountry");
call.enqueue(new Callback<CommonProto.Country>() {
#Override
public void onResponse(Call<CommonProto.Country> call, Response<CommonProto.Country> response) {
String bodyString = null;
try {
Log.e("RETROFIT ::::::: ", String.valueOf(response.body())+"TEST");
} catch (Exception e) {
Log.e("RETROFIT ERROR ::::::: ", e.getMessage()+"TEST");
e.printStackTrace();
}
}
#Override
public void onFailure(Call<CommonProto.Country> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
}
);
when i run this way i got the error
java.lang.RuntimeException: com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
my Proto.java file and also have Proto.proto file both are here in this link,
https://drive.google.com/folderview?id=0B4loQuzINvHCRUlNbk5LUXE1NXM&usp=sharing
Please let me know how to do this GET Req and also I was Struggling with POST Req.
you can create interface like this
public interface LoginInterface {
#FormUrlEncoded
#POST("url goes here")
Call<LoginResponseData> getUserLoginDeatail(#FieldMap Map<String, String> fields);
}
make an instance of retro file and call interface method something like this
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("base url")
.build();
webApis = retrofit.create(WebApis.class);
Call<LoginResponseData> call = webApis.getCurrentRide(keyValue);
call.enqueue(new Callback<LoginResponseData>() {
#Override
public void onResponse(Call<LoginResponseData> call, Response<LoginResponseData> response) {
try {
} catch (Exception e) {
// customizedToast.showToast(context.getResources().getString(
// R.string.exception));
e.printStackTrace();
}
}
#Override
public void onFailure(Call<LoginResponseData> call, Throwable t) {
}
});
for protocol buffer you can find a reference here

Categories