What is the possible error for this retrofit code? I can't understand the connection of my API and MOBILE
You almost reached to the solution. But, you made little mistake while passing parameters to the API request.
As I can see from the screenshot of Insomia app, that API requires JSONArray as parameter but you're sending JSONObject.
Sample JSON parameter
[ { "SerialNumber" : "1234", "Coordinate1" : "12.7845874", "Coordinate2" : "76.4584578", "DateTime" : "2018-11-14 08:45:00", "Speed" : "0", "Port" : 9090 }]
According to the above JSON structure you need to change the Api.java
class to something like this:
import retrofit2.Call;import retrofit2.http.Body;import retrofit2.http.POST;import java.util.List; // add importpublic interface Api { @POST("/api/Database/NewLocation") Call < MapDetails > mapDetailLocation(@Body List<MapDetails> mapDetails); // ^^^^ changes here @POST("/api/Registration/RegisterDevice") Call < RegisterDetails > registerDetails(@Body RegisterDetails registerAllDetails);}
Add List<MapDetails>
to mapDetailLocation()
method parameter.
And in Activity or Fragment use above method like this:
//......// part of the codeMapDetails mapDetails = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);List<MapDetails> data = new ArrayList<>();data.add(mapDetails);Retrofit.Builder builder = new Retrofit.Builder() .baseUrl("<BASE_URL>") // change base URL .addConverterFactory(GsonConverterFactory.create());Retrofit retrofit = builder.build();Api locate = retrofit.create(Api.class);Call<MapDetails> call = locate.mapDetailLocation(data); // NOTICE THE CHANGES IN PARAMETERcall.enqueue(new Callback<MapDetails>() { @Override public void onResponse(Call<MapDetails> call, Response<MapDetails> response) { // do whatever you want } @Override public void onFailure(Call call, Throwable t) { // log the error message }});
Note: Please change base URL according to your requirement.
Edit:
Change method parameters in Activity from MapDetails
to List<MapDetails>
// prepare dataMapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);// add it to ArrayListList<MapDetails> mapDetails = new ArrayList<>();mapDetails.add(data);// pass it as an argumentprivate void setLocation(List<MapDetails> mapDetails) { initializeRetrofit(mapDetails);}
Change method parameter in initializeRetrofit()
private void initializeRetrofit(List<MapDetails> mapDetails) { Retrofit.Builder builder = new Retrofit.Builder() .baseUrl("<BASE_URL>") // change base URL .addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder.build(); Api locate = retrofit.create(Api.class); SetMapLocationApiCaller(locate, mapDetails);}
Again change method parameter
private void SetMapLocationApiCaller(Api locate, List<MapDetails> mapDetails) { Call<MapDetails> call = locate.mapDetailLocation(mapDetails); executeCallAsynchronously(call);}