What is the possible error for this retrofit code? I can't understand the connection of my API and MOBILE
I would like to find out the error is from my code or from my api.
This is my API CLASS:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface Api
@POST("/api/Database/NewLocation")
Call<MapDetails> mapDetailLocation(@Body MapDetails mapDetails);
@POST("/api/Registration/RegisterDevice")
Call<RegisterDetails> registerDetails(@Body RegisterDetails
registerAllDetails);
SETTER CLASS:
import com.google.gson.annotations.SerializedName;
public class MapDetails
@SerializedName("SerialNumber")
private String serialNumber;
@SerializedName("Coordinate1")
private String coordinate1;
@SerializedName("Coordinate2")
private String coordinate2;
@SerializedName("DateTime")
private String dateTime;
@SerializedName("Speed")
private String speed;
@SerializedName("Port")
private Integer Port;
public MapDetails(String serialNumber, String coordinate1, String
coordinate2,
String dateTime, String speed, Integer port)
this.serialNumber = serialNumber;
this.coordinate1 = coordinate1;
this.coordinate2 = coordinate2;
this.dateTime = dateTime;
this.speed = speed;
Port = port;
public String getSerialNumber()
return serialNumber;
public void setSerialNumber(String serialNumber)
this.serialNumber = serialNumber;
public String getCoordinate1()
return coordinate1;
public void setCoordinate1(String coordinate1)
this.coordinate1 = coordinate1;
public String getCoordinate2()
return coordinate2;
public void setCoordinate2(String coordinate2)
this.coordinate2 = coordinate2;
public String getDateTime()
return dateTime;
public void setDateTime(String dateTime)
this.dateTime = dateTime;
public String getSpeed()
return speed;
public void setSpeed(String speed)
this.speed = speed;
public Integer getPort()
return Port;
public void setPort(Integer port)
Port = port;
Activity Class:
MapDetails mapDetails = new MapDetails("1807200005",
lat,lon, currentDateTimeString, "0", 9090);
setLocation(mapDetails);
private void setLocation(MapDetails mapDetails)
initializeRetrofit(mapDetails);
private void initializeRetrofit(MapDetails mapDetails)
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://undefine.apisecure.data[![enter image description here][1]][1]")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Api locate = retrofit.create(Api.class);
SetMapLocationApiCaller(locate, mapDetails);
private void SetMapLocationApiCaller(Api locate, MapDetails
mapDetails)
Call<MapDetails> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
private void executeCallAsynchronously(Call call)
call.enqueue(new Callback<MapDetails>()
@Override
public void onResponse(Call<MapDetails> call,
Response<MapDetails> response)
Snackbar.make(view,""+ response,
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
@Override
public void onFailure(Call call, Throwable t)
Snackbar.make(view, ""+t.getMessage(),
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
);
On my app , this is the response on my app:
but its not added to my sql. But if am using insomia, it would have send the data I've created to the database.
this data was inserted via insomia not on mobile.
json api android-studio retrofit2 okhttp3
add a comment |
I would like to find out the error is from my code or from my api.
This is my API CLASS:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface Api
@POST("/api/Database/NewLocation")
Call<MapDetails> mapDetailLocation(@Body MapDetails mapDetails);
@POST("/api/Registration/RegisterDevice")
Call<RegisterDetails> registerDetails(@Body RegisterDetails
registerAllDetails);
SETTER CLASS:
import com.google.gson.annotations.SerializedName;
public class MapDetails
@SerializedName("SerialNumber")
private String serialNumber;
@SerializedName("Coordinate1")
private String coordinate1;
@SerializedName("Coordinate2")
private String coordinate2;
@SerializedName("DateTime")
private String dateTime;
@SerializedName("Speed")
private String speed;
@SerializedName("Port")
private Integer Port;
public MapDetails(String serialNumber, String coordinate1, String
coordinate2,
String dateTime, String speed, Integer port)
this.serialNumber = serialNumber;
this.coordinate1 = coordinate1;
this.coordinate2 = coordinate2;
this.dateTime = dateTime;
this.speed = speed;
Port = port;
public String getSerialNumber()
return serialNumber;
public void setSerialNumber(String serialNumber)
this.serialNumber = serialNumber;
public String getCoordinate1()
return coordinate1;
public void setCoordinate1(String coordinate1)
this.coordinate1 = coordinate1;
public String getCoordinate2()
return coordinate2;
public void setCoordinate2(String coordinate2)
this.coordinate2 = coordinate2;
public String getDateTime()
return dateTime;
public void setDateTime(String dateTime)
this.dateTime = dateTime;
public String getSpeed()
return speed;
public void setSpeed(String speed)
this.speed = speed;
public Integer getPort()
return Port;
public void setPort(Integer port)
Port = port;
Activity Class:
MapDetails mapDetails = new MapDetails("1807200005",
lat,lon, currentDateTimeString, "0", 9090);
setLocation(mapDetails);
private void setLocation(MapDetails mapDetails)
initializeRetrofit(mapDetails);
private void initializeRetrofit(MapDetails mapDetails)
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://undefine.apisecure.data[![enter image description here][1]][1]")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Api locate = retrofit.create(Api.class);
SetMapLocationApiCaller(locate, mapDetails);
private void SetMapLocationApiCaller(Api locate, MapDetails
mapDetails)
Call<MapDetails> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
private void executeCallAsynchronously(Call call)
call.enqueue(new Callback<MapDetails>()
@Override
public void onResponse(Call<MapDetails> call,
Response<MapDetails> response)
Snackbar.make(view,""+ response,
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
@Override
public void onFailure(Call call, Throwable t)
Snackbar.make(view, ""+t.getMessage(),
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
);
On my app , this is the response on my app:
but its not added to my sql. But if am using insomia, it would have send the data I've created to the database.
this data was inserted via insomia not on mobile.
json api android-studio retrofit2 okhttp3
add a comment |
I would like to find out the error is from my code or from my api.
This is my API CLASS:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface Api
@POST("/api/Database/NewLocation")
Call<MapDetails> mapDetailLocation(@Body MapDetails mapDetails);
@POST("/api/Registration/RegisterDevice")
Call<RegisterDetails> registerDetails(@Body RegisterDetails
registerAllDetails);
SETTER CLASS:
import com.google.gson.annotations.SerializedName;
public class MapDetails
@SerializedName("SerialNumber")
private String serialNumber;
@SerializedName("Coordinate1")
private String coordinate1;
@SerializedName("Coordinate2")
private String coordinate2;
@SerializedName("DateTime")
private String dateTime;
@SerializedName("Speed")
private String speed;
@SerializedName("Port")
private Integer Port;
public MapDetails(String serialNumber, String coordinate1, String
coordinate2,
String dateTime, String speed, Integer port)
this.serialNumber = serialNumber;
this.coordinate1 = coordinate1;
this.coordinate2 = coordinate2;
this.dateTime = dateTime;
this.speed = speed;
Port = port;
public String getSerialNumber()
return serialNumber;
public void setSerialNumber(String serialNumber)
this.serialNumber = serialNumber;
public String getCoordinate1()
return coordinate1;
public void setCoordinate1(String coordinate1)
this.coordinate1 = coordinate1;
public String getCoordinate2()
return coordinate2;
public void setCoordinate2(String coordinate2)
this.coordinate2 = coordinate2;
public String getDateTime()
return dateTime;
public void setDateTime(String dateTime)
this.dateTime = dateTime;
public String getSpeed()
return speed;
public void setSpeed(String speed)
this.speed = speed;
public Integer getPort()
return Port;
public void setPort(Integer port)
Port = port;
Activity Class:
MapDetails mapDetails = new MapDetails("1807200005",
lat,lon, currentDateTimeString, "0", 9090);
setLocation(mapDetails);
private void setLocation(MapDetails mapDetails)
initializeRetrofit(mapDetails);
private void initializeRetrofit(MapDetails mapDetails)
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://undefine.apisecure.data[![enter image description here][1]][1]")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Api locate = retrofit.create(Api.class);
SetMapLocationApiCaller(locate, mapDetails);
private void SetMapLocationApiCaller(Api locate, MapDetails
mapDetails)
Call<MapDetails> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
private void executeCallAsynchronously(Call call)
call.enqueue(new Callback<MapDetails>()
@Override
public void onResponse(Call<MapDetails> call,
Response<MapDetails> response)
Snackbar.make(view,""+ response,
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
@Override
public void onFailure(Call call, Throwable t)
Snackbar.make(view, ""+t.getMessage(),
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
);
On my app , this is the response on my app:
but its not added to my sql. But if am using insomia, it would have send the data I've created to the database.
this data was inserted via insomia not on mobile.
json api android-studio retrofit2 okhttp3
I would like to find out the error is from my code or from my api.
This is my API CLASS:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface Api
@POST("/api/Database/NewLocation")
Call<MapDetails> mapDetailLocation(@Body MapDetails mapDetails);
@POST("/api/Registration/RegisterDevice")
Call<RegisterDetails> registerDetails(@Body RegisterDetails
registerAllDetails);
SETTER CLASS:
import com.google.gson.annotations.SerializedName;
public class MapDetails
@SerializedName("SerialNumber")
private String serialNumber;
@SerializedName("Coordinate1")
private String coordinate1;
@SerializedName("Coordinate2")
private String coordinate2;
@SerializedName("DateTime")
private String dateTime;
@SerializedName("Speed")
private String speed;
@SerializedName("Port")
private Integer Port;
public MapDetails(String serialNumber, String coordinate1, String
coordinate2,
String dateTime, String speed, Integer port)
this.serialNumber = serialNumber;
this.coordinate1 = coordinate1;
this.coordinate2 = coordinate2;
this.dateTime = dateTime;
this.speed = speed;
Port = port;
public String getSerialNumber()
return serialNumber;
public void setSerialNumber(String serialNumber)
this.serialNumber = serialNumber;
public String getCoordinate1()
return coordinate1;
public void setCoordinate1(String coordinate1)
this.coordinate1 = coordinate1;
public String getCoordinate2()
return coordinate2;
public void setCoordinate2(String coordinate2)
this.coordinate2 = coordinate2;
public String getDateTime()
return dateTime;
public void setDateTime(String dateTime)
this.dateTime = dateTime;
public String getSpeed()
return speed;
public void setSpeed(String speed)
this.speed = speed;
public Integer getPort()
return Port;
public void setPort(Integer port)
Port = port;
Activity Class:
MapDetails mapDetails = new MapDetails("1807200005",
lat,lon, currentDateTimeString, "0", 9090);
setLocation(mapDetails);
private void setLocation(MapDetails mapDetails)
initializeRetrofit(mapDetails);
private void initializeRetrofit(MapDetails mapDetails)
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("http://undefine.apisecure.data[![enter image description here][1]][1]")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
Api locate = retrofit.create(Api.class);
SetMapLocationApiCaller(locate, mapDetails);
private void SetMapLocationApiCaller(Api locate, MapDetails
mapDetails)
Call<MapDetails> call =
locate.mapDetailLocation(mapDetails);
executeCallAsynchronously(call);
private void executeCallAsynchronously(Call call)
call.enqueue(new Callback<MapDetails>()
@Override
public void onResponse(Call<MapDetails> call,
Response<MapDetails> response)
Snackbar.make(view,""+ response,
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
@Override
public void onFailure(Call call, Throwable t)
Snackbar.make(view, ""+t.getMessage(),
Snackbar.LENGTH_INDEFINITE)
.setAction("Action", null).show();
);
On my app , this is the response on my app:
but its not added to my sql. But if am using insomia, it would have send the data I've created to the database.
this data was inserted via insomia not on mobile.
json api android-studio retrofit2 okhttp3
json api android-studio retrofit2 okhttp3
edited Nov 14 '18 at 6:16
syam
7761925
7761925
asked Nov 14 '18 at 0:51
Julius AndayaJulius Andaya
5410
5410
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 import
public 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 code
MapDetails 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 PARAMETER
call.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 data
MapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);
// add it to ArrayList
List<MapDetails> mapDetails = new ArrayList<>();
mapDetails.add(data);
// pass it as an argument
private 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);
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Insomnia screenshot shows that you're sendingJSONArray
which starts with[
and ends with]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.
– Shashanth
Nov 14 '18 at 5:13
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291629%2fwhat-is-the-possible-error-for-this-retrofit-code-i-cant-understand-the-connec%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 import
public 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 code
MapDetails 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 PARAMETER
call.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 data
MapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);
// add it to ArrayList
List<MapDetails> mapDetails = new ArrayList<>();
mapDetails.add(data);
// pass it as an argument
private 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);
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Insomnia screenshot shows that you're sendingJSONArray
which starts with[
and ends with]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.
– Shashanth
Nov 14 '18 at 5:13
|
show 6 more comments
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 import
public 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 code
MapDetails 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 PARAMETER
call.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 data
MapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);
// add it to ArrayList
List<MapDetails> mapDetails = new ArrayList<>();
mapDetails.add(data);
// pass it as an argument
private 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);
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Insomnia screenshot shows that you're sendingJSONArray
which starts with[
and ends with]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.
– Shashanth
Nov 14 '18 at 5:13
|
show 6 more comments
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 import
public 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 code
MapDetails 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 PARAMETER
call.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 data
MapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);
// add it to ArrayList
List<MapDetails> mapDetails = new ArrayList<>();
mapDetails.add(data);
// pass it as an argument
private 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);
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 import
public 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 code
MapDetails 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 PARAMETER
call.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 data
MapDetails data = new MapDetails("1807200005", lat, lon, currentDateTimeString, "0", 9090);
// add it to ArrayList
List<MapDetails> mapDetails = new ArrayList<>();
mapDetails.add(data);
// pass it as an argument
private 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);
edited Nov 14 '18 at 5:43
answered Nov 14 '18 at 4:46
ShashanthShashanth
2,51442236
2,51442236
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Insomnia screenshot shows that you're sendingJSONArray
which starts with[
and ends with]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.
– Shashanth
Nov 14 '18 at 5:13
|
show 6 more comments
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Insomnia screenshot shows that you're sendingJSONArray
which starts with[
and ends with]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.
– Shashanth
Nov 14 '18 at 5:13
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
ive chage it with my local api
– Julius Andaya
Nov 14 '18 at 5:07
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
upon the mobile, it passes true but not added in my database.... upon using insomnia(like postman), if i send the json object created i finnaly add the data on my database...
– Julius Andaya
Nov 14 '18 at 5:09
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
also i've try to postman sending json array via post on my api url... but its not working, only in insomnia via json object.
– Julius Andaya
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Did you try exact what I've mentioned in my answer?
– Shashanth
Nov 14 '18 at 5:10
Insomnia screenshot shows that you're sending
JSONArray
which starts with [
and ends with ]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.– Shashanth
Nov 14 '18 at 5:13
Insomnia screenshot shows that you're sending
JSONArray
which starts with [
and ends with ]
. If that works perfectly, you should read my answer properly (with comments). Check parameters of the method that I've mentioned in my answer. Use the same base URL which you're trying with Insomnia.– Shashanth
Nov 14 '18 at 5:13
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291629%2fwhat-is-the-possible-error-for-this-retrofit-code-i-cant-understand-the-connec%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown