choi choi - 22 days ago 6
Android Question

How do i store the json response from my server to an arraylist and pass to next activity?

I am using Retrofit and the server returns

JSON
object as response. I want to store it in an
ArrayList
and pass it to the next activity where it can be displayed in
RecyclerView
. I am not getting any error but there is no output on the Display activity. What could be the problem?

Here is my main activity.

public class MainActivity extends AppCompatActivity {


public static final String ROOT_URL = "http://walletuncle.com/";

String username;
String password;

private Button makerequest;

private ProgressDialog pdialog;

private EditText edittextusername;
private EditText edittextpassword;

private String DataArray[] = new String[15];

private ArrayList<Result> dataarr = new ArrayList<>();

int i = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

makerequest = (Button) findViewById(R.id.btnObjRequest);


edittextpassword = (EditText) findViewById(R.id.editpassword);
edittextusername = (EditText) findViewById(R.id.editusername);

pdialog = new ProgressDialog(this);
pdialog.setMessage("wait kar be...");
pdialog.setCancelable(false);


makerequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
makejsonobjectrequest();
}
});


}


private void makejsonobjectrequest() {


username = edittextusername.getText().toString().trim();
password = edittextpassword.getText().toString().trim();


RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL)
.build();


//creating object of our interface
DataApi data = adapter.create(DataApi.class);

//defining the method.
data.getposition(username, password, new Callback<Result>() {


@Override
public void success(Result result, Response response) {


dataarr = result;


Bundle b = new Bundle();
b.putSerializable("array", dataarr);
Intent intent = new Intent(getBaseContext(), Display.class);
intent.putExtras(b);
startActivity(intent);
}

@Override
public void failure(RetrofitError error) {

}
});
}


}


my interface for retrofit is:

public interface DataApi {

//retrofit to get annotation with our url.

@GET("/myservice.asmx/UserLogin")
void getposition(@Query("UserId") String userid, @Query("password") String password, Callback<Result> result);

}

Answer

instead of Callback<ArrayList<String>> cb

use this Callback<Result> result in your interface ...

and do changes in your Retrofit call also .

instead of this <ArrayList<String>>

use like this <Result>.....

use this POJO class ....

public class Result {

@SerializedName("phoneno")
@Expose
private String phoneno;
@SerializedName("comission")
@Expose
private String comission;
@SerializedName("Status")
@Expose
private String status;
@SerializedName("memberid")
@Expose
private String memberid;
@SerializedName("mid")
@Expose
private Integer mid;
@SerializedName("CARD")
@Expose
private String cARD;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("msgcount")
@Expose
private String msgcount;
@SerializedName("WalletAmount")
@Expose
private String walletAmount;
@SerializedName("mtype")
@Expose
private Integer mtype;
@SerializedName("RewardPoints")
@Expose
private Integer rewardPoints;
@SerializedName("totalcusto")
@Expose
private String totalcusto;
@SerializedName("monthcusto")
@Expose
private String monthcusto;
@SerializedName("notify")
@Expose
private Boolean notify;
@SerializedName("notification")
@Expose
private String notification;

/**
* 
* @return
* The phoneno
*/
public String getPhoneno() {
return phoneno;
}

/**
* 
* @param phoneno
* The phoneno
*/
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}

/**
* 
* @return
* The comission
*/
public String getComission() {
return comission;
}

/**
* 
* @param comission
* The comission
*/
public void setComission(String comission) {
this.comission = comission;
}

/**
* 
* @return
* The status
*/
public String getStatus() {
return status;
}

/**
* 
* @param status
* The Status
*/
public void setStatus(String status) {
this.status = status;
}

/**
* 
* @return
* The memberid
*/
public String getMemberid() {
return memberid;
}

/**
* 
* @param memberid
* The memberid
*/
public void setMemberid(String memberid) {
this.memberid = memberid;
}

/**
* 
* @return
* The mid
*/
public Integer getMid() {
return mid;
}

/**
* 
* @param mid
* The mid
*/
public void setMid(Integer mid) {
this.mid = mid;
}

/**
* 
* @return
* The cARD
*/
public String getCARD() {
return cARD;
}

/**
* 
* @param cARD
* The CARD
*/
public void setCARD(String cARD) {
this.cARD = cARD;
}

/**
* 
* @return
* The name
*/
public String getName() {
return name;
}

/**
* 
* @param name
* The Name
*/
public void setName(String name) {
this.name = name;
}

/**
* 
* @return
* The msgcount
*/
public String getMsgcount() {
return msgcount;
}

/**
* 
* @param msgcount
* The msgcount
*/
public void setMsgcount(String msgcount) {
this.msgcount = msgcount;
}

/**
* 
* @return
* The walletAmount
*/
public String getWalletAmount() {
return walletAmount;
}

/**
* 
* @param walletAmount
* The WalletAmount
*/
public void setWalletAmount(String walletAmount) {
this.walletAmount = walletAmount;
}

/**
* 
* @return
* The mtype
*/
public Integer getMtype() {
return mtype;
}

/**
* 
* @param mtype
* The mtype
*/
public void setMtype(Integer mtype) {
this.mtype = mtype;
}

/**
* 
* @return
* The rewardPoints
*/
public Integer getRewardPoints() {
return rewardPoints;
}

/**
* 
* @param rewardPoints
* The RewardPoints
*/
public void setRewardPoints(Integer rewardPoints) {
this.rewardPoints = rewardPoints;
}

/**
* 
* @return
* The totalcusto
*/
public String getTotalcusto() {
return totalcusto;
}

/**
* 
* @param totalcusto
* The totalcusto
*/
public void setTotalcusto(String totalcusto) {
this.totalcusto = totalcusto;
}

/**
* 
* @return
* The monthcusto
*/
public String getMonthcusto() {
return monthcusto;
}

/**
* 
* @param monthcusto
* The monthcusto
*/
public void setMonthcusto(String monthcusto) {
this.monthcusto = monthcusto;
}

/**
* 
* @return
* The notify
*/
public Boolean getNotify() {
return notify;
}

/**
* 
* @param notify
* The notify
*/
public void setNotify(Boolean notify) {
this.notify = notify;
}

/**
* 
* @return
* The notification
*/
public String getNotification() {
return notification;
}

/**
* 
* @param notification
* The notification
*/
public void setNotification(String notification) {
this.notification = notification;
}

}

EDIT:-

use this code instead of your

private Result result;  // create this as global


 data.getposition(username, password, new Callback<Result>() {


            @Override
            public void success(Result res, Response response) {

                result = res;


            }

            @Override
            public void failure(RetrofitError error) {

            }
        });