tux-world tux-world - 25 days ago 12
Android Question

Retrofit get JsonArray with GSON as default parser

this result is send by web service and i try to parse it by simple class which that is like with result structure,

{
"result":
[
{
"id": "386434",
"weight": "72.000",
"type": "xxxxx",
"shamsi_date": "1395/8/5",
"date": "2016-10-26 10:52:47"
},
{
"id": "395118",
"weight": "70.000",
"type": "xxxx",
"shamsi_date": "1395/8/19",
"date": "2016-11-09 10:58:29"
}
],
"message": ""
}


as you can see
result
is JsonArray and i would like to get it and put to
JSONARRAY
, and after that i'm trying get
JSONObject
s from that.

UPDATED CODE:



private void getUserAllVisits(String userId) {
UserAllVisits userAllVisits = new UserAllVisits();
userAllVisits.setUserId(userId);

Call<UserAllVisits> call = behandamService.getUserAllVisitsCall(userAllVisits);
call.enqueue(new Callback<UserAllVisits>() {
@Override
public void onResponse(Call<UserAllVisits> call, final Response<UserAllVisits> response) {
if (response.isSuccessful()) {
JSONArray jsonArray = new JSONArray(Arrays.asList(response.body().getResult()));
}
}

@Override
public void onFailure(Call<UserAllVisits> call, Throwable t) {
Log.e("Err: ", t.getMessage());
}
});
}


in my code
response.body().getResult().length;
return
2
and i can get
result
array with
response.body().getResult();
but i can't define simple variable as
JSONArray
and assign that to variable as:

JSONArray jsonArray = new JSONArray(response.body().getResult());


I get error and i can't do that

public class UserAllVisits {
private String userId;
private UserAllVisitsResult[] result;
private String message;

public class UserAllVisitsResult {
private String date;
private String shamsi_date;
private String weight;
private String id;
private String type;

public String getDate() {
return this.date;
}

public void setDate(String date) {
this.date = date;
}

public String getShamsi_date() {
return this.shamsi_date;
}

public void setShamsi_date(String shamsi_date) {
this.shamsi_date = shamsi_date;
}

public String getWeight() {
return this.weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}
}


public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public UserAllVisitsResult[] getResult() {
return this.result;
}

public void setResult(UserAllVisitsResult[] result) {
this.result = result;
}

public String getMessage() {
return this.message;
}

public void setMessage(String message) {
this.message = message;
}
}


and
UserVisits
class:

public class UserVisits extends RealmObject {
private String date;
private String shamsi_date;
private String weight;
private String id;
private String type;

public String getDate() {
return this.date;
}

public void setDate(String date) {
this.date = date;
}

public String getShamsi_date() {
return this.shamsi_date;
}

public void setShamsi_date(String shamsi_date) {
this.shamsi_date = shamsi_date;
}

public String getWeight() {
return this.weight;
}

public void setWeight(String weight) {
this.weight = weight;
}

public String getId() {
return this.id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return this.type;
}

public void setType(String type) {
this.type = type;
}
}


UPDATE with service Provider:



import com.kermany.behandam.Service.ClientConfigs;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class BehAndamWebServiceProvider {
private BehAndamServiceService mTService;

public BehAndamWebServiceProvider() {

OkHttpClient httpClient = new OkHttpClient();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ClientConfigs.REST_API_BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
mTService = retrofit.create(BehAndamServiceService.class);
}

public BehAndamServiceService getTService() {
return mTService;
}
}

Answer

Use ResponseBody instead of your Pojo class.Try this:

Call<ResponseBody> call =    behandamService.getUserAllVisitsCall(userAllVisits);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
        if (response.isSuccessful()) {

            //handle Exceptions here
            String jsonString = response.body().string();
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray jsonarray = jsonObject.getJSONArray("result");
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("Err: ", t.getMessage());
    }
});

and your interface should be like this

@POST // or GET method as per your requirement
Call<ResponseBody> yourmethod(Your parameters);