Reputation: 95
I got an Array:
[{"stammkost":"IWXI","call":"name1","ean":"802.6180.222","number":"5"},{"stammkost":"8566","call":"name2","ean":"657.7121.393","number":"5"}]
I want to send a PHP-Call for every Object in it like this (using the asynctask for it. This works for one reason not right. The Asynctask should executed for every object in the array, right? But it only gets executed the first time, or like logcat the last time. The tablenames are right, but obvious the asynctask doesnt get executed right.. Can you help me?
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":"[email protected]","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":"[email protected]","out_date":"15.04.2015"}'MYSQL Error: Table 'IWAA_8566_15.04.2015' already exists
the function:
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();
}
}
}
and the 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){
}
}
EDIT:
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;
Upvotes: 1
Views: 37
Reputation: 43322
Edit:
I think that the GlobalClass.tablename
might be out of sync with the multiple AsyncTask
instances.
Try passing the arguments in through the varargs
, here is an example:
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();
}
}
}
In your 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){
}
}
Upvotes: 1