2016-11-19 1 views
2

Ich benutze Retrofit und der Server gibt JSON Objekt als Antwort zurück. Ich möchte es in einem ArrayList speichern und es an die nächste Aktivität übergeben, wo es in RecyclerView angezeigt werden kann. Ich bekomme keinen Fehler, aber es gibt keine Ausgabe in der Anzeige Aktivität. Was könnte das Problem sein?Wie speichere ich die JSON-Antwort von meinem Server an eine Arraylist und gehe zur nächsten Aktivität über?

Hier ist meine Haupttätigkeit.

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) { 

     } 
    }); 
} 


} 

meine Schnittstelle zur Nachrüstung ist:

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

Überprüfen Sie mit Protokoll, kommt Ihre Antwort oder nicht? – sushildlh

+0

E/dalvikvm: Konnte die Klasse 'android.graphics.drawable.RippleDrawable' nicht finden, die von der Methode android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering referenziert wurde – choi

+0

Dies scheint ein Fehler zu sein, den ich beim Debugging gefunden habe – choi

Antwort

2

statt Callback<ArrayList<String>> cb

Verwendung dieses Callback<Result> result in Ihrer Schnittstelle ...

und machen Veränderungen in Ihrem auch Retrofit Anruf.

anstelle dieses <ArrayList<String>>

Verwendung wie diese <Result> .....

Verwendung dieser POJO Klasse ....

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; 
} 

} 

EDIT: -

Verwenden Sie diesen Code anstelle Ihrer

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) { 

      } 
     }); 
+0

Wie speichere ich die Antwort in einer Arraylist oder einem Array? – choi

+0

wo Sie das Array speichern müssen, gibt es kein Array innerhalb des JSON .... – sushildlh

+0

Ich möchte jeden Wert in einer Arraylist speichern, so dass ich es in Recyclerview anzeigen kann. – choi

2

Verschieben dieser Block in Erfolgsmethode. Da es sich um einen Async-Anruf handelt, erhalten Sie das Ergebnis nicht sofort, warten Sie also auf die Antwort und geben Sie sie dann an eine andere Aktivität weiter.

@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); 
} 
+0

Jetzt öffnet sich die nächste Aktivität nicht, was bedeutet, dass keine erfolgreiche Antwort vom Server vorliegt. was ist da falsch? – choi

+0

Also die Grundidee ist nicht auf den nächsten Bildschirm zu gehen, bis Sie eine positive Antwort bekommen. – Rahul

+0

Nachrüstung: Nachrüstung: 500 interner Serverfehler. – choi

Verwandte Themen