这是我的android程序。我想先将id=1
和name=0
保存到数据库中。然后,当我再次点击一个按钮时,它将变为id=1
和name=1
。但即使我使用count ++,我的数据库仍会显示id=1
和name=0
。为什么name=0
而不是name=1
?
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AndroidTestActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
private int mCount= 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ArrayList nameValuePairs = new ArrayList();
mCount++;
nameValuePairs.add(new BasicNameValuePair("id","1"));
nameValuePairs.add(new BasicNameValuePair("name",Integer.toString (mCount)));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://192.168.1.132/insert.php");
httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpost);
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
}
});
}
}
答案 0 :(得分:0)
获取名称后,您正在增加计数。您需要在此之前完成:
mCount++;
nameValuePairs.add(new BasicNameValuePair("id","1"));
nameValuePairs.add(new BasicNameValuePair("name",Integer.toString (mCount)));