0

这是我的安卓程序。我想先保存id=1name=0进入数据库。然后当我再次单击一个按钮时,它将变为id=1and name=1。但是我的数据库仍然显示id=1name=0即使我使用 count++。为什么是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());
          }
         }
       });
  }
 }
4

1 回答 1

0

获得名称后,您正在增加计数。您需要先执行以下操作:

mCount++;
nameValuePairs.add(new BasicNameValuePair("id","1"));
nameValuePairs.add(new BasicNameValuePair("name",Integer.toString (mCount)));
于 2013-10-16T05:11:01.520 回答