我有一个数组:
[{"stammkost":"IWXI","call":"name1","ean":"802.6180.222","number":"5"},{"stammkost":"8566","call":"name2","ean":"657.7121.393","number":"5"}]
我想像这样为它中的每个Object发送一个PHP-Call(使用asynctask for it。这有一个原因不正确.Asynctask应该为数组中的每个对象执行,对吧?但它只会得到第一次执行,或者最后一次像logcat一样。表名是正确的,但很明显asynctask没有正确执行..你能帮助我吗?
logcat的:
04-15 21:01:54.207: V/Button(3938): Send
04-15 21:01:54.207: V/stammkost(3938): IWXI
04-15 21:01:54.207: V/tablename(3938): IWAA_IWXI_15.04.2015
04-15 21:01:54.207: V/stammkost(3938): 8566
04-15 21:01:54.207: V/tablename(3938): IWAA_8566_15.04.2015
04-15 21:01:54.207: V/jsArray(3938): [{"ean":"802.6180.222","number":"5"},{"ean":"657.7121.393","number":"5"}]
04-15 21:01:54.407: D/CreateMovementAsyncTask(3938): CREATE TABLE `IWAA_8566_15.04.2015` (uid int(11) primary key auto_increment, unique_id varchar(23) not null unique, ean varchar(20) not null, number varchar(6) not null,accepted varchar(1) not null) comment='{"out_user":"pb","out_email":"test@test.com","out_date":"15.04.2015"}'1
04-15 21:01:54.447: D/CreateMovementAsyncTask(3938): CREATE TABLE `IWAA_8566_15.04.2015` (uid int(11) primary key auto_increment, unique_id varchar(23) not null unique, ean varchar(20) not null, number varchar(6) not null,accepted varchar(1) not null) comment='{"out_user":"pb","out_email":"test@test.com","out_date":"15.04.2015"}'MYSQL Error: Table 'IWAA_8566_15.04.2015' already exists
功能:
private void CreateMovement(){
for (int i = 0; i < GlobalClass.jsArrayGeraete.length(); i++) {
try {
JSONObject jsonObj = GlobalClass.jsArrayGeraete.getJSONObject(i);
GlobalClass.KOST_NEW = jsonObj.getString("stammkost");
GlobalClass.tablename = SelectKostActivity.KOST + "_" + GlobalClass.KOST_NEW + "_" + GlobalClass.date;
new CreateMovementAsyncTask().execute();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
和AsyncTask:
class CreateMovementAsyncTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... arg0) {
DBFunctions DBFunction = new DBFunctions();
try {
JSONObject jObjComment = new JSONObject();
jObjComment.put("out_user", LoginActivity.name);
jObjComment.put("out_email", GlobalClass.email);
jObjComment.put("out_date", GlobalClass.date);
String json = DBFunction.create_movement(GlobalClass.tablename,jObjComment.toString());
Log.d("CreateMovementAsyncTask", json);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
protected void onPostExecute(Void json){
}
}
编辑:
public String create_movement(String tablename, String comment){
// Building Parameters
//Log.e("tablename", tablename);
//Log.e("comment", comment);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", "create_movement"));
params.add(new BasicNameValuePair("tablename", tablename));
params.add(new BasicNameValuePair("comment", comment));
ServiceHandler jsonStr = new ServiceHandler();
String json = jsonStr.makeServiceCall(requesteanURL, ServiceHandler.POST, params);
return json;
PHP:
else if ($tag == 'create_movement') {
$coni=mysqli_connect("localhost","LOGIN","PW","movement");
$tablename = $_POST['tablename'];
$comment = $_POST['comment'];
//$jcomment = json_decode($comment, true);
$sql = "CREATE TABLE `" .$tablename. "` (uid int(11) primary key auto_increment, unique_id varchar(23) not null unique, ean varchar(20) not null, betriebszahl varchar(6) not null,accepted varchar(1) not null) comment='".$comment."'";
echo $sql;
$result = mysqli_query($coni, $sql) or die("Query failed : " . mysqli_error($coni));
echo $result;
答案 0 :(得分:1)
编辑:
我认为GlobalClass.tablename
可能与多个AsyncTask
实例不同步。
尝试通过varargs
传递参数,这是一个例子:
private void CreateMovement(){
for (int i = 0; i < GlobalClass.jsArrayGeraete.length(); i++) {
try {
JSONObject jsonObj = GlobalClass.jsArrayGeraete.getJSONObject(i);
GlobalClass.KOST_NEW = jsonObj.getString("stammkost");
//GlobalClass.tablename = SelectKostActivity.KOST + "_" + GlobalClass.KOST_NEW + "_" + GlobalClass.date;
String tableName = SelectKostActivity.KOST + "_" + GlobalClass.KOST_NEW + "_" + GlobalClass.date;
//pass in the table name and data
new CreateMovementAsyncTask().execute(tableName, GlobalClass.email, GlobalClass.date);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在AsyncTask中:
class CreateMovementAsyncTask extends AsyncTask<String,Void,Void>{
protected Void doInBackground(String... arg0) {
DBFunctions DBFunction = new DBFunctions();
try {
JSONObject jObjComment = new JSONObject();
jObjComment.put("out_user", LoginActivity.name);
jObjComment.put("out_email", arg0[1]); //use varargs instead
jObjComment.put("out_date", arg0[2]); //use varargs instead
//String json = DBFunction.create_movement(GlobalClass.tablename,jObjComment.toString());
String json = DBFunction.create_movement(arg0[0], jObjComment.toString());
Log.d("CreateMovementAsyncTask", json);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
protected void onPostExecute(Void json){
}
}