我正在使用Retrofit,服务器返回JSON
个对象作为响应。我想将其存储在ArrayList
中并将其传递到下一个可以在RecyclerView
中显示的活动。我没有收到任何错误,但Display活动中没有输出。可能是什么问题?
这是我的主要活动。
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) {
}
});
}
}
我的改造界面是:
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);
}
答案 0 :(得分:2)
以成功方式移动此块。作为异步调用,您将无法立即获得结果,因此请等待响应,然后将其传递给另一个 Activity 。
@Override
public void success(ArrayList<String> strings, Response response) {
dataarr = strings;
Bundle b = new Bundle();
b.putStringArrayList("array", dataarr);
Intent intent = new Intent(getBaseContext(), Display.class);
intent.putExtras(b);
startActivity(intent);
}
答案 1 :(得分:2)
而不是
Callback<ArrayList<String>> cb
在您的界面中使用此
Callback<Result> result
...并在您的Retrofit电话中进行更改。
而不是
<ArrayList<String>>
像这样使用
<Result>
.....
使用此POJO类......
public class Result implements Serialization {
@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;
}
}
修改: - 强>
使用此代码代替
private Result result; // create this as global
data.getposition(username, password, new Callback<Result>() {
@Override
public void success(Result res, Response response) {
result = res;
Intent intent = new Intent(getBaseContext(), Display.class);
intent.putExtra("data",(Serializable)result);
startActivity(intent);
}
@Override
public void failure(RetrofitError error) {
}
});