我的方法showTrajects
我循环我id
的所有trajects
,直到这里好。
编辑: 我一直在寻找并且问题总是得到AllPendingTrajects的最后一个ID,但为什么它没有进入第一个或第二个?
问题出在AllPendingTrajects()
:
public void showTrajects() {
for (indexTrajecte = 0; indexTrajecte < contador; indexTrajecte++) {
AllPendingTrajects();
}
}
public void AllPendingTrajects(){
AsyncHttpClient client = new AsyncHttpClient();
String URL = "http://url.net/api/link1/"+IDTrajecte[indexTrajecte];
client.get(URL,new AsyncHttpResponseHandler() {
@Override
public void onStart() {}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
JSONObject trajectes = new JSONObject();
String strResponseBody = new String(responseBody);
try {
trajectes = new JSONObject(strResponseBody);
}catch (JSONException e){
Toast.makeText(PendingTrajectRecyclerView.this, "Error", Toast.LENGTH_SHORT).show();
}
try {
JSONArray realitzas = trajectes.getJSONArray("realitzas");
dniTaxista = trajectes.getString("taxistes_dni");
idTraject = Integer.parseInt(trajectes.getString("ID"));
if(DadesTaxista.DNI.equals(dniTaxista)){
showPendingTraject();
}
else {
SeeAllTrajects();
}
}catch (JSONException e){
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody, Throwable error) {
Log.d("ERROR",""+statusCode);
}
});
}
当我在日志中输入方法SeeAllTrajects()
时,我总是得到一个随机ID 而我不知道可能是什么,这是没有意义的,我试过了创建一个全局变量但没有成功
public void SeeAllTrajects(){
AsyncHttpClient client = new AsyncHttpClient();
String URL = "http://url.net/api/link2";
client.get(URL,new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
JSONArray trajectesJSON = new JSONArray();
String strResponseBody = new String(responseBody);
try {
trajectesJSON = new JSONArray(strResponseBody);
}catch (JSONException e){
Toast.makeText(PendingTrajectRecyclerView.this, "Error", Toast.LENGTH_SHORT).show();
}
try {
Log.d("ID TRAJECT:","d"+idByTraject);
}
}catch (JSONException e){
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d("ERROR",""+statusCode);
}
});
}