what is wrong with this retrofit ? request is success but no values are get
this is how my json looks like
[
{
"msg": "test",
"time": "2017-06-20 00:39:31",
"kind": "txt"
},
{
"msg": "مرحبا يا برو",
"time": "2017-06-20 00:40:02",
"kind": "txt"
},
{
"msg": "url",
"time": "2017-06-20 01:57:12",
"kind": "img"
},
{
"msg": "url",
"time": "2017-06-20 01:58:54",
"kind": "video"
}
]
AllMessagesResponse class
public class AllMessagesResponse {
public ArrayList<MessagesInfo> getMessagesInfos() {
return messagesInfos;
}
private ArrayList<MessagesInfo>messagesInfos=new ArrayList<>();
}
MessageInfo class
public class MessagesInfo{
#SerializedName("time")
private String time;
#SerializedName("msg")
private String msgs;
#SerializedName("kind")
private String kind;
#SerializedName("sender")
private String senderID;
public String getTime() {
return time;
}
public String getMsgs() {
return msgs;
}
public String getKind() {
return kind;
}
public String getSenderID() {
return senderID;
}
}
API class
#POST("chat/veiwPeerToPeer.php")
Call<AllMessagesResponse>getMessages(#Body AllMessages allMessages);
Main Activity class
AllMessages allMessages=new AllMessages();
allMessages.senderID=MainActivity.userId;
allMessages.receiverID=receiverId;
WebService.getInstance().getApi().getMessages(allMessages).enqueue(new Callback<AllMessagesResponse>() {
#Override
public void onResponse(Call<AllMessagesResponse> call, Response<AllMessagesResponse> response) {
AllMessagesResponse allMessagesResponse=response.body();
setTitle(String.valueOf(allMessagesResponse.getMessagesInfos().size()));
}
#Override
public void onFailure(Call<AllMessagesResponse> call, Throwable t) {
}
});
onResponse is working but I get no values
You don't need another pojo for list you can directly request for list Retrofit directly provide list.
Call<List<POJO>>
I fetch the same JSON response like you did. SEE URL
apiInterface = new Retrofit.Builder()
.baseUrl(BASE_URL_ANDROIDHIVE)
.addConverterFactory(GsonConverterFactory.create())
.build().create(ApiInterface.class);
final Call<List<AndroidHive>> hiveCall = apiInterface.getAllMovies();
hiveCall.enqueue(new Callback<List<AndroidHive>>() {
#Override
public void onResponse(Call<List<AndroidHive>> call, Response<List<AndroidHive>> response) {
if (response.isSuccessful()) {
progressDialog.dismiss();
List<AndroidHive> hiveList = response.body();
tv_retrofit.setText("Title : " + hiveList.get(3).getTitle() + "\n Image : " + hiveList.get(3).getImage() + "\n Rating : " + hiveList.get(3).getRating() + " \n Release Year : " + hiveList.get(3).getReleaseYear() + "\n Gener : " + hiveList.get(3).getGenre());
} else {
Toast.makeText(getActivity(), "Response Failed Code : " + response.message(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
#Override
public void onFailure(Call<List<AndroidHive>> call, Throwable t) {
System.out.println(t.getStackTrace().toString());
progressDialog.dismiss();
}
});
Related
I want to show a district list in a spinner, and i am getting that data through an API.
Here is an example of the response.
{
"districts": [
{
"district": "ANANTNAG"
},
{
"district": "BADGAM"
},
{
"district": "BANDIPORA"
},
{
"district": "BARAMULLA"
},
{
"district": "DODA"
},
{
"district": "GANDERBAL"
},
{
"district": "JAMMU"
},
{
"district": "KARGIL"
},
{
"district": "KATHUA"
},
{
"district": "KISHTWAR"
},
{
"district": "KULGAM"
},
{
"district": "KUPWARA"
},
{
"district": "LEH LADAKH"
},
{
"district": "POONCH"
},
{
"district": "PULWAMA"
},
{
"district": "RAJAURI"
},
{
"district": "RAMBAN"
},
{
"district": "REASI"
},
{
"district": "SAMBA"
},
{
"district": "SHOPIAN"
},
{
"district": "SRINAGAR"
},
{
"district": "UDHAMPUR"
},
{
"district": "JAMMU AND KASHMIR"
}
],
"Request_type": "districts",
"responseCode": "Success"
}
After that, I created 2 schemapojo class for response and dataList as well.
Then, I am calling this into a class but its not working!! I don't have any idea how to call an API and set the response data into a spinner.
Please help
private void getdistricts(String state) {
JSONObject mJobj = new JSONObject();
try {
mJobj.put("state", state);
mJobj.put(REQUEST, "districts");
} catch (JSONException e) {
e.printStackTrace();
}
Call<districtresponse> call = RetrofitClient.getInstance().getApi().getDistrict(mJobj);
call.enqueue(new Callback<districtresponse>() {
#Override
public void onResponse(Call<districtresponse> call, Response<districtresponse> response) {
String status = response.body().getResponseCode();
if (status.equalsIgnoreCase("Success")){
district_list = response.body().getDistricts();
districts = new ArrayAdapter<String>(this,R.layout.dist_list,district_list);
}
}
#Override
public void onFailure(Call<districtresponse> call, Throwable t) {
}
});
}
Here in code district_List variable is for a second model class where all district List is available.
I tried a different process from youtube but none of that worked !!
Change code
districts = new ArrayAdapter<String>(this,R.layout.dist_list,district_list);
To this
ArrayAdapter adapter
= new ArrayAdapter(
this,
android.R.layout.simple_spinner_item,
districts);
// set simple layout resource file
// for each item of spinner
adapter.setDropDownViewResource(
android.R.layout
.simple_spinner_dropdown_item);
// Set the ArrayAdapter (ad) data on the
// Spinner which binds data to spinner
spino.setAdapter(adapter);
You can also try like this
ArrayList<String> stringArrayList = null; //Create A global Variable
Call<Example> call = RetrofitClient.getInstance().getApi().getDistrict();
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
if (response.code() == 200) {
ArrayList<District> districts = (ArrayList<District>) response.body().getDistricts();
for (int i = 0; i < districts.size(); i++) {
stringArrayList.add(districts.get(i).getDistrict());
}
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
}
});
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.dist_list,district_list,stringArrayList);
arrayAdapter.setDropDownViewResource(R.layout.dist_list,district_list);
yourSpinner.setAdapter(arrayAdapter);
And here create a model like this
Example Model Class
public class Example {
#SerializedName("districts")
#Expose
private List<District> districts = null;
#SerializedName("Request_type")
#Expose
private String requestType;
#SerializedName("responseCode")
#Expose
private String responseCode;
public List<District> getDistricts() {
return districts;
}
public void setDistricts(List<District> districts) {
this.districts = districts;
}
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getResponseCode() {
return responseCode;
}
public void setResponseCode(String responseCode) {
this.responseCode = responseCode;
}
}
District Model Class
public class District {
#SerializedName("district")
#Expose
private String district;
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
}
I just hope your problem is solved using this
I am trying to convert the keys in my json string to camelCase..i have gone trough various posts in the stackoverflow but couldn't come to the solution..
i have a json string coming in the below format..
{
"tech":[
{
"id":"1",
"company_name":"Microsoft",
"country_of_origin":"USA"
},
{
"id":"2",
"company_name":"SAP",
"country_of_origin":"Germany"
}
],
"Manufacturing":[
{
"id":"3",
"company_name":"GM",
"country_of_origin":"USA"
},
{
"id":"4",
"company_name":"BMW",
"country_of_origin":"Germany"
}
]
}
Expected Response
{
"tech":[
{
"id":"1",
"companyName":"Microsoft",
"countryOfOrigin":"USA"
},
{
"id":"2",
"companyName":"SAP",
"countryOfOrigin":"Germany"
}
],
"Manufacturing":[
{
"id":"3",
"companyName":"GM",
"countryOfOrigin":"USA"
},
{
"id":"4",
"companyName":"BMW",
"countryOfOrigin":"Germany"
}
]
}
i have written a jsonDeserializer class based on previous post in stackoverflow..
Gson gson = new GsonBuilder()
.registerTypeAdapter(UpperCaseAdapter.TYPE, new UpperCaseAdapter())
.create();
Map<String, List<Company>> mapDeserialized = gson.fromJson(jsonString, UpperCaseAdapter.TYPE);
System.out.println(mapDeserialized);
And the deserilizer
public class UpperCaseAdapter implements JsonDeserializer<Map<String, Object>> {
public static final Type TYPE = new TypeToken<Map<String, Object>>() {}.getType();
#Override
public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Map<String, Object> map = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
Object value = null;
if (entry.getValue().isJsonPrimitive()) {
value = entry.getValue().getAsString();
} else if (entry.getValue().isJsonObject()) {
value = context.deserialize(entry.getValue(), TYPE);
} else if (entry.getValue().isJsonArray()) {
for(JsonElement jsonElement:entry.getValue().getAsJsonArray()){
value=context.deserialize(jsonElement,TYPE);
}
} else if (entry.getValue().isJsonNull()) {
continue;
}
map.put(CaseFormat.LOWER_UNDERSCORE.to(
CaseFormat.LOWER_CAMEL, entry.getKey()), value);
}
return map;
}
}
Model class
public class Company {
private String id;
private String compnayName;
private String countryOfOrigin;
}
When i use the above deserilizer though it is able to convert the keys to camle case for some json array object....i can see that it is only doing that for one jsonobject in each array and not taking other array objects in to consideration.as shown below.
Wrong Response i am getting with above serializer(missing other jsonobjecsts and the key manufacturing is converted to lower):
{
"tech="{
"companyName=SAP",
id=2,
"countryOfOrigin=Germany"
},
"manufacturing="{
"companyName=BMW",
id=4,
"countryOfOrigin=Germany"
}
}
I know there are lot of posts available in stackoverflow and i have to this extent solely based on those posts but as i am new to Java and json serilization i couldn't progress anymore...any help in this regard is greatly appreciated thanks in advance
you can use the following code to get the CamelCase string;
static public class Company {
private String id;
private String companyName;
private String countryOfOrigin;
public Company() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCountryOfOrigin() {
return countryOfOrigin;
}
public void setCountryOfOrigin(String countryOfOrigin) {
this.countryOfOrigin = countryOfOrigin;
}
}
#Test
void t4() throws JsonProcessingException {
ObjectMapper mapperSC = new ObjectMapper();
mapperSC.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE);
String data = "{ " +
" \"tech\":[ " +
" { " +
" \"id\":\"1\", " +
" \"company_name\":\"Microsoft\", " +
" \"country_of_origin\":\"USA\" " +
" }, " +
" { " +
" \"id\":\"2\", " +
" \"company_name\":\"SAP\", " +
" \"country_of_origin\":\"Germany\" " +
" } " +
" ], " +
" \"Manufacturing\":[ " +
" { " +
" \"id\":\"3\", " +
" \"company_name\":\"GM\", " +
" \"country_of_origin\":\"USA\" " +
" }, " +
" { " +
" \"id\":\"4\", " +
" \"company_name\":\"BMW\", " +
" \"country_of_origin\":\"Germany\" " +
" } " +
" ] " +
"}";
TypeFactory tf = mapper.getTypeFactory();
JavaType jt = tf.constructMapType(Map.class, tf.constructType(String.class), tf.constructArrayType(Company.class));
Object o = mapperSC.readValue(data, jt);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(o));
}
The output will be;
{
"tech" : [ {
"id" : "1",
"companyName" : "Microsoft",
"countryOfOrigin" : "USA"
}, {
"id" : "2",
"companyName" : "SAP",
"countryOfOrigin" : "Germany"
} ],
"Manufacturing" : [ {
"id" : "3",
"companyName" : "GM",
"countryOfOrigin" : "USA"
}, {
"id" : "4",
"companyName" : "BMW",
"countryOfOrigin" : "Germany"
} ]
}
I'm working on project in which I have to implement on API. This API repsonse is objects of ArrayList. Can you please help me with creating its POJO class and if possible its implementation. I'm using retrofit2 & GSON.
As shown in following JSON schema, brand names will be added in brandsonly by admin, and it will be added in allorders as a arraylist which have multiple sub-objects.
Like, if admin add Redmi in the brandsonly then it will create Redmi[] in the allorders
{
"status": "success",
"brandsonly": [
{
"_id": "",
"brandname": "SAMSUNG",
},
{
"_id": "",
"brandname": "VIVO",
},
{
"_id": "",
"brandname": "NOKIA"
},
{
"_id": "",
"brandname": "IPHONE",
}
],
"allorders": {
"SAMSUNG": [],
"VIVO": [],
"NOKIA": [],
"IPHONE": [
{
"_id": "",
"order_id": "",
"__v": 0,
"adminconfirmation": 1,
"finalpricetodeduct": 30950
},
{
"_id": "",
"order_id": "",
"__v": 0,
"adminconfirmation": 1,
"finalpricetodeduct":
}
]
},
}
My Retrofit call from activity:
final AllOrdersResponse allOrdersResponse = new AllOrdersResponse(userID);
Call<AllOrdersResponse> responseCall = retrofit_interface.allOrderResponse(allOrdersResponse, "Bearer " + AuthToken);
responseCall.enqueue(new Callback<AllOrdersResponse>() {
#Override
public void onResponse(#NotNull Call<AllOrdersResponse> call, #NotNull Response<AllOrdersResponse> response) {
AllOrdersResponse response1 = response.body();
}
#Override
public void onFailure(#NotNull Call<AllOrdersResponse> call, #NotNull Throwable t) {
if (t instanceof SocketTimeoutException)
Toast.makeText(context, "Socket Time out. Please try again.", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
}
});
I dont think a POJO schema would work in this instance as it will always change. It would be much better is the allorders was the same as brandsonly a JSON Array of JSON Array
However, if that cannot be changed have a look at the below.
final AllOrdersResponse allOrdersResponse = new AllOrdersResponse(userID);
Call<AllOrdersResponse> responseCall = retrofit_interface.allOrderResponse(allOrdersResponse, "Bearer " + AuthToken);
responseCall.enqueue(new Callback<AllOrdersResponse>() {
#Override
public void onResponse(#NotNull Call<AllOrdersResponse> call, #NotNull Response<AllOrdersResponse> response) {
AllOrdersResponse response1 = response.body();
List<String> brands = new ArrayList<>();
List<Map<String, Product>> products = new ArrayList<>();
JSONObject jsonObject = new JSONObject(response1);
for(JSONObject brand : jsonObject.get("brandsonly")){
brands.add(brand.getvalue("brandname"));
}
if(brands.size= > 0){
for(String brandname: brands){
HashMap<String, Product> tempHash = new HashMap<>();
JSONArray temp = jsonObject.getJSONArray(brandname);
foreach(JSONObject x : temp){
Product product = new Product();
product.FromJSONObject(x);
temp.put(brandname, product);
}
products.add(tempHash);
}
}
}
#Override
public void onFailure(#NotNull Call<AllOrdersResponse> call, #NotNull Throwable t) {
if (t instanceof SocketTimeoutException)
Toast.makeText(context, "Socket Time out. Please try again.", Toast.LENGTH_LONG).show();
else
Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
}
});
So you have a list of BandNames with a value of each product.
I would also recommend looking at jsonschema2.pojo
All,
I have following JSON response after request get list of users. I want to pull just only one user's id using userName. For example if i want id of userName Test1, how to do that? Any help will be appreciated.
{
"displayLength": "4",
"iTotal": "20",
"users": [
{
"id": "2",
"userName": "Test1",
"Group": { id:1
name:"Test-Admin"
}
},
{
"id": "17",
"userName": "Test2",
"Group": { id:1
name:"Test-Admin"
}
},
{
"id": "32",
"userName": "Test3",
"Group": { id:1
name:"Test-Admin"
}
},
{
"id": "35",
"userName": "Test4",
"Group": { id:1
name:"Test-Admin"
}
}
]
}
Thanks,
See if this below code helps. Just pass user name to userName variable and let the code find the userId for you.
JSONObject json = new JSONObject(" {\n" + " \"displayLength\": \"4\",\n"
+ " \"iTotal\": \"20\",\n" + " \"users\": [\n" + " {\n"
+ " \"id\": \"2\",\n" + " \"userName\": \"Test1\",\n"
+ " \"Group\": { id:1,\n" + " name:\"Test-Admin\"\n"
+ " }\n" + " },\n" + " {\n" + " \"id\": \"17\",\n"
+ " \"userName\": \"Test2\",\n" + " \"Group\": { id:1,\n"
+ " name:\"Test-Admin\"\n" + " }\n" + " },\n"
+ " {\n" + " \"id\": \"32\",\n" + " \"userName\": \"Test3\",\n"
+ " \"Group\": { id:1,\n" + " name:\"Test-Admin\"\n"
+ " }\n" + " },\n" + " {\n" + " \"id\": \"35\",\n"
+ " \"userName\": \"Test4\",\n" + " \"Group\": { id:1,\n"
+ " name:\"Test-Admin\"\n" + " }\n" + " } \n"
+ "\n" + " ]\n" + " }");
JSONArray array = json.getJSONArray("users");
String userName = "Test1";
Integer userId = null;
for (int i = 0; i < array.length() && userId == null; i++) {
JSONObject jsonIn = (JSONObject) array.get(i);
if (jsonIn.optString("userName").equals(userName)) {
userId = jsonIn.optInt("id");
}
}
System.out.println("User ID for User Name '" + userName + "' is : " + userId);
I recomend use http-request built on apache http api. You must create class ResponseData to parse response.
private static final HttpRequest<ResponseData> HTTP_REQUEST =
HttpRequestBuilder.createGet(yourUri, ResponseData.class).build();
public void test() {
HTTP_REQUEST.execute().ifHasContent(responseData -> {
Optional<User> foundedUser = responseData.getUsers()
.stream()
.filter(user -> "Test1".equals(user.getUserName()))
.findFirst();
foundedUser.ifPresent(user -> System.out.println(user.getId()));
});
}
class ResponseData {
private int displayLength;
private int iTotal;
private List<User> users;
public int getDisplayLength() {
return displayLength;
}
public void setDisplayLength(int displayLength) {
this.displayLength = displayLength;
}
public int getiTotal() {
return iTotal;
}
public void setiTotal(int iTotal) {
this.iTotal = iTotal;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
class User {
private int id;
private String userName;
private Group Group;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Group getGroup() {
return Group;
}
public void setGroup(Group group) {
Group = group;
}
}
class Group {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Note: Your json is incorrect. See Corrected:
{
"displayLength": "4",
"iTotal": "20",
"users": [
{
"id": "2",
"userName": "Test1",
"Group": {
"id": 1,
"name": "Test-Admin"
}
},
{
"id": "17",
"userName": "Test2",
"Group": {
"id": 1,
"name": "Test-Admin"
}
},
{
"id": "32",
"userName": "Test3",
"Group": {
"id": 1,
"name": "Test-Admin"
}
},
{
"id": "35",
"userName": "Test4",
"Group": {
"id": 1,
"name": "Test-Admin"
}
}
]
}
jsonObj.getJsonArray("users") and then convert the array to list. Now use Java 8 Stream and Filter api's to extract the desired output.
I have a response form server as :
[
{
"ID": "1",
"Title": "BIRATNAGAR",
"BankID": "1",
"BranchCode": "0",
"LocationID": "71500200",
"IsActive": "yes"
},
{
"ID": "2",
"Title": "BIRATNAGAR",
"BankID": "1",
"BranchCode": "0",
"LocationID": "71500900",
"IsActive": "yes"
},
{
"ID": "3",
"Title": "BIRATNAGAR",
"BankID": "1",
"BranchCode": "0",
"LocationID": "94361117",
"IsActive": "yes"
}
]
I have retrofit api as:
#POST("authapp/Restserver/api/Masterdata/getBranchListByBank")
Call> getBranchListByBank(#Query("api_key") String id);
I have called it as:
RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
Call<List<BankBranch>> call = service.getBranchListByBank(s);
call.enqueue(new Callback<List<BankBranch>>() {
#Override
public void onResponse(Call<List<BankBranch>> call, Response<List<BankBranch>> response) {
try {
List<BankBranch> banks = response.body();
for (int i = 0; i < banks.size(); i++) {
String id = banks.get(i).getTitle();
String name = banks.get(i).getID();
String marks = banks.get(i).getIsActive();
Log.i("ashihs", id + " " + marks + " " + name);
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
#Override
public void onFailure(Call<List<BankBranch>> call, Throwable t) {
Log.d("onFailure", t.toString());
}
});
But I cannot get the list of the bank branch. I get error as :
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Can any one help me?
Thanks in advance.
U probably got bad Api request it schould look like this:
api:
#POST("authapp/Restserver/api/Masterdata/getBranchListByBank")
Call<List<BankBranch>> getBranchListByBank(#Query("api_key") String id);
And the method schould look like this:
Call<List<BankBranch>> response = apiCall.getBranchListByBank(id);
response.enqueue(new Callback<List<BankBranch>>() {
#Override
public void onResponse(Call<List<BankBranch>> call, Response<List<BankBranch>> response) {
List<BankBranch> bankBranch = response.body();
}
#Override
public void onFailure(Call<List<BankBranch>> call, Throwable t) {
}
});
}
make sure Your model BankBranch fit the JSON response;