在获取我将值存储在本地数据库中的值之后,我试图使用 AsyncHttpClient 在 for 循环中调用 php 文件。
我正在向我的 php 文件发送参数,因为它需要从 mySQL 获取数据。获取部分工作正常,但是在存储数据时我遇到了问题。我实际上是存储通过 php 接收的值,然后将参数值添加到同一个数据库表中。
问题是,当我尝试获取数据时,循环发送正确的参数值,但是当接收到 JSONARRAY 数据并将其存储到 DB 参数值时,它始终是 for 循环中的最后一个数字。这是代码和示例:
private void getSubMenuValues()
{
for (int i = 0; i < localdb.getCountValue(); i++)
{
String Id2 = localdb.getValue().get(i).getID();
Log.e("DB","Value"+Id2); (All the id2 value is printed immeidately in Log.)
parent_ID = Id2;
Log.e("Outside Async","Value"+parent_ID);
final RequestParams requestParams = new RequestParams();
requestParams.put("SId", Id2);
final String uploadWebsite = url_grant_id_details;
AsyncHttpClient client = new AsyncHttpClient();
client.post(uploadWebsite, requestParams, new JsonHttpResponseHandler()
{
private JSONArray details;
private int success;
private String ImageURL;
private String IsChild;
private String SId;
private String SName;
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response)
{
try
{
alldetails = response.getJSONArray("alldetail");
for (int i = 0; i < details.length(); i++)
{
JSONObject c = alldetails.getJSONObject(i);
SId = c.getString("SId");
SName = c.getString("SName");
IsChild = c.getString("IsChild");
ImageURL = c.getString("ImageURL");
Log.e("Inside HTTP REQUEST","Value"+parent_ID); (Value is always the last number in the loop)
localdb.setProfileDetails(SId, SName, IsChild, ImageURL, parent_ID);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse)
{
super.onFailure(statusCode, headers, throwable, errorResponse);
Toast.makeText(Main.this, "Unable to connect to server. Check your network !!", Toast.LENGTH_SHORT).show();
}
});
}
}
这里 parent_id = id2 之前的第一个日志会立即打印所有值,而不是一个一个地打印。
像这样的东西:
03-15 12:36:27.060: E/DB(16205): Value 4
03-15 12:36:27.099: E/DB(16205): Value 3
03-15 12:36:27.130: D/dalvikvm(16205): GC_FOR_ALLOC freed 244K, 3% free 10191K/10472K, paused 16ms, total 16ms
03-15 12:36:27.146: E/DB(16205): Value 5
03-15 12:36:27.154: E/DB(16205): Value 63
03-15 12:36:27.169: E/DB(16205): Value 62
03-15 12:36:27.185: I/Choreographer(16205): Skipped 40 frames! The application may be doing too much work on its main thread.
03-15 12:36:27.701: V/AsyncHttpResponseHandler(16205): Progress 1376 from 628 (219%)
03-15 12:36:27.701: V/AsyncHttpResponseHandler(16205): Progress 1669 from 628 (266%)
03-15 12:36:27.716: E/Inside HTTP Request(16205): Value62
03-15 12:36:27.755: E/Inside HTTP Request(16205): Value62
03-15 12:36:27.787: E/Inside HTTP Request16205): Value62
如果您检查日志: DB 值立即打印 4,3,5,63,62 然后在 http 请求之后它始终是循环中的值 62。
我不确定到底发生了什么?
有人可以帮助我了解为什么会发生这种情况以及如何在循环中调用异步 http 请求吗?
谢谢!