Android Retrofit 2 - problem with sending the Array<Object> using POST - java

im new with retrofit and now, when i know how to sent the normal data without any objects, just with parameters or simple body i want to know how to sent the objects...
I spent like 20h to debug it and i'm confused because i dont know how to do this...
There is my codes:
API Interface:
#POST("/api/assortment")
Call<PostAssortment> getAssortment(#Body String PostShipmentProgress);
PostAssortment class:
public class PostAssortment {
private String JSON;
#SerializedName("token")
#Expose
private String token;
#SerializedName("assortment")
#Expose
private Assortment assortment;
#SerializedName("tokens")
#Expose
private List<String> tokens;
#SerializedName("positions")
#Expose
private List<Position> positions;
#SerializedName("deviceId")
#Expose
private String deviceId;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Shipment getAssortment() {
return assortment;
}
public void setAssortment(Assortment assortment) {
this.assortment = assortment;
}
public List<String> getTokens() {
return tokens;
}
public void setTokens(List<String> tokens) {
this.tokens = tokens;
}
public List<Position> getPositions() {
return positions;
}
public void setPositions(List<Position> positions) {
this.positions = positions;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getJSON() {
return JSON;
}
public void setJSON(String JSON) {
this.JSON = JSON;
}
}
And the mainJava class:
Gson gson = new Gson();
PostAssortment postAssortment= new PostAssortment();
List<String> tokens = new ArrayList<>();
tokens.add("someToken");
postAssortment.setTokens(tokens);
postAssortment.setDeviceId("aaaaa");
List<Position> currentPosition = new ArrayList<>();
Position cp = new Position();
cp.setItemName("Some");
cp.setPlace("POLAND");
cp.setTimestamp("2020-12-09T11:00:00");
currentPosition.add(cp);
postAssortment.setPositions(currentPosition);
String postAssortmentJSON = gson.toJson(postAssortment);
Call<PostAssortment> call = ApiLoginInterface.getAssortment(postAssortmentJSON);
call.enqueue(new Callback<PostAssortment>() {
#Override
public void onResponse(Call<PostAssortment> call, Response<PostAssortment> response) {
PostAssortment assortmentResponse = response.body();
}
#Override
public void onFailure(Call<PostAssortment> call, Throwable t) {
Log.d("FAILURE", "onFailure: " + t.getMessage());
}
});
}
And my retrofit onCreate:
Gson gson = new GsonBuilder()
.setLenient()
.create();
String BASE_URL = getString(API_URL);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ApiLoginInterface = retrofit.create(ApiLoginInterface.class);
And after im trying to call it im not getting any point on call enqueue just a
Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
Error...
Can someone describe this and help me to make it work? :/

You haven't provided enough information to help identify the error. Probably add the full stacktrace to the question as well. But if your API post request is expecting a json body I would start with the fixes below:
Remove this:
String postAssortmentJSON = gson.toJson(postAssortment);
Then pass your object as a pojo to your retrofit interface like this:
#POST("/api/assortment")
Call<PostAssortment> getAssortment(#Body PostAssortment postAssortment);
Then when doing your call you don't need to convert it to a string json string. The adapter does that for you:
Call<PostAssortment> call = ApiLoginInterface.getAssortment(postAssortment);

Post assortment in my problem will be in a list, so to make it works I need to change the Call to

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.

How to POST an ArrayList with a String at the same time in RETROFIT?

I'm trying to post an ArrayList and a String value in Retrofit. How can I send them at the same post ?
I've tried this but it didn't work.
Thank you.
Etiket_post.java
#POST("/api/r_etiket")
Call<Result> post_etiket(#Body List< EtiketItem_List> items, #Body String FileNo);
Print_Screen.java
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("myURL")
.addConverterFactory(GsonConverterFactory.create())
.build();
Etiket_Post etiket_post = retrofit.create(Etiket_Post.class);
String FileNo = FileId;
ArrayList<EtiketItem_List> items = new ArrayList<>();
for (int e = 0; e < okutulan_list.size(); e++) {
items.add(new EtiketItem_List(
okutulan_list.get(e).STOK_KODU,
okutulan_list.get(e).STOK_ADI,
okutulan_list.get(e).OlcuBrim,
okutulan_list.get(e).STHAR_GCMIK));
}
Call<Result> call = etiket_post.post_etiket(items, FileNo);
call.enqueue(new Callback<Result>() {
#Override
public void onResponse(Call<Result> call, Response<Result> response) {
Toast.makeText(Print_Screen.this, response.body().result, Toast.LENGTH_LONG).show();
}
#Override
public void onFailure(Call<Result> call, Throwable t) {
Toast.makeText(Print_Screen.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
You can send data in raw format using Retrofit2 like this.
{
"String1" :"anyString",
"arrayList":[
{
"A":"Appple",
"B":"ball"
}
]
}
modelClass example
#SerializedName("user_id")
#Expose
private String user_id;
#SerializedName("product")
#Expose
List<OneProductModel> oneProductModels;
public CartPost(String user_id, List<OneProductModel> oneProductModels) {
this.user_id = user_id;
this.oneProductModels = oneProductModels;
}
#POST("/api/r_etiket")
Call<Result> post_etiket(#Body modelClass);

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;
}
}

How to parse JSON arrays with retrofit

I have JSON file like this http://androiddocs.ru/api/friends.json
{
"data":"dbfriends",
"friends": [{"id":"1","name":"Andrew","city":"Moscow","contacts":{"mobile":"+7 0000000","email":"andrew#androiddocs.ru","skype":"andrew"}}, {"id":"2","name":"Ivan","city":"Kiev","contacts":{"mobile":"+38 0000000","email":"ivan#androiddocs.ru","skype":"ivan"}}]
}
my retrofit interface
public interface Friends_API {
String BASE_URL = " http://androiddocs.ru/api/";
#GET("friends.json") Call<Friends> getFriends();
class Factory {
private static Friends_API service;
public static Friends_API getInstance(){
if (service == null) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build();
service = retrofit.create(Friends_API.class);
return service;
} else {
return service;
}
}
}
}
POJO file generated by http://www.jsonschema2pojo.org/
and my code to get data value and:
Friends_API.Factory.getInstance().getFriends().enqueue(new Callback<Friends>() {
#Override
public void onResponse(Call<Friends> call, Response<Friends> response) {
String getData = response.body().getData();
}
#Override
public void onFailure(Call<Friends> call, Throwable t) {
}
}
i can't understood how i can get value: id, name, city...
thanks for help!
You can get the data from retrieved response using:
Friends friends = response.body();
And then you can retrieve the List<Friend> using friends.getFriends() object. Now you can iterate through this list of friends and can get id,name, city or contact details.

Best way to model Android REST connection

I am making an Android app that connects to a web service via REST API and I have a dilemma with the design of the internal architecture.
Now I have class Client.java whose purpouse is make connect with the server (ConnectionMethod is Enum that contains GET|POST values):
public class Client {
private AsyncHttpClient client = new AsyncHttpClient(); //I use com.loopj.AsyncHttpClient to connect
private ConnectionMethod method;
private RequestParams params = new RequestParams();
private AsyncHttpResponseHandler responseHandler = new JsonHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//Actions when connection success
}
#Override
public void onFailure(int statusCode, Header[] headers, JSONObject response, Throwable error) {
//Actions when connection fails
}
};
public Client (RequestParams params, ConnectionMethod method) {
this.params = params;
this.method = method;
}
public void addParameters (Map<String, String> parameters) {
for (Map.Entry<String, String> entry : parameters.entrySet()) {
this.params.put(entry.getKey(), entry.getValue());
}
}
public ServerResponse connect () {
RequestHandle handle;
if (this.method==ConnectionMethod.POST) {
handle = postRequest();
}
else {
handle = getRequest();
}
//How can I treat here different type of responses homogeneously?
}
private RequestHandle getRequest () {
return client.get(Constants.getEndpoint(), this.params, this.responseHandler);
}
private RequestHandle postRequest () {
return client.post(Constants.getEndpoint(), this.params, this.responseHandler);
}
}
A sample method that requests info from the server is this:
public static void login (String login, String password) {
//This classes should be static or dynamic?
Map<String, String> map = new HashMap<String, String>();
map.put("login", login);
map.put("password", password);
map.put("method", "site_login");
Client c = new Client();
c.addParameters(map);
c.getRequest();
}
All server responses are JSON: {status:0, result:array/int/string} when response is correct and {status:-1, message:string} when response is incorrect.
Additionaly I want to make classes to model components from JSON result (User.java, Message.java...) and intermediate methods between UI and API to implement the logic of the app and classes.
What is the best way to design an homogeneous connection system that manages automatically correct/fail response and independent of model (user, message...)?
There is a bunch of frameworks which can make this whole process much easier.
For example Retrofit is very simple framework for mapping java classes to REST calls. It comes with gson which will automatically deserialize response from json to plain java objects.
It also allows use callbacks as well as rxJava Observables. It allows to handle errors as well.
You can check sample app: https://github.com/JakeWharton/u2020
You are describing tools that already exist. My favorite happens to be Retrofit but there are others out there. Retrofit can handle the success and fail responses and even map JSON directly to a POJO.
My API client
public class ApiClient {
private static ApiInterface sApiInterface;
public static ApiInterface getApiClient(Context context) {
//build the rest adapter
if (sApiInterface == null) {
final RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("example.com")
.build();
sApiInterface = restAdapter.create(ApiInterface.class);
}
return sApiInterface;
}
public interface ApiInterface {
#GET("/program/{id}")
void getProgram(#Path("id") int id, RetrofitCallback<Program> callback);
}
My RetrofitCallback
public class RetrofitCallback<S> implements Callback<S> {
private static final String TAG = RetrofitCallback.class.getSimpleName();
#Override
public void success(S s, Response response) {
}
#Override
public void failure(RetrofitError error) {
Log.e(TAG, "Failed to make http request for: " + error.getUrl());
Response errorResponse = error.getResponse();
if (errorResponse != null) {
Log.e(TAG, errorResponse.getReason());
if (errorResponse.getStatus() == 500) {
Log.e(TAG, "Handle Server Errors Here");
}
}
}
}
My model
public class Program {
#Expose
private doublea.models.Airtime Airtime;
#Expose
private String id;
#Expose
private String title;
#SerializedName("short_name")
#Expose
private String shortName;
#SerializedName("full_description")
#Expose
private String fullDescription;
#SerializedName("short_description")
#Expose
private String shortDescription;
#Expose
private doublea.models.Image Image;
#SerializedName("image")
#Expose
private String imageName;
#Expose
private List<Host> hosts = new ArrayList<Host>();
#Expose
private List<Category> categories = new ArrayList<Category>();
#Expose
private List<Airtime> airtimes = new ArrayList<Airtime>();
/** Getters and Setters */
public Program() {
}
How it is used.
private void executeProgramApiCall(int programId) {
ApiClient.getApiClient(this).getProgram(programId, new RetrofitCallback<Program>() {
#Override
public void success(Program program, Response response) {
super.success(program, response);
addDataToAdapter(program);
}
});
}

Categories