0

在我的应用程序的第一个活动中,我使用以下代码将数据存储到 mysql 中。Java代码:

public class MainActivity extends Activity {

EditText et;
Button b;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    et = (EditText) findViewById(R.id.editText1);
    b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            String name = et.getText().toString();
            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));


            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/insert1.php");
                httppost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Toast.makeText(getBaseContext(), "Congrats! ur added", Toast.LENGTH_LONG).show();

                Intent mainpage = new Intent("com.example.test.PLACE");
                startActivity(mainpage);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "Not connected to DataBase", Toast.LENGTH_LONG).show();
            }

        }
    });

}

为了简单起见,我在这里只使用了一个字段。现在我尝试在其他活动中从用户那里获取另一个字段,并尝试使用上述代码的相同概念将其存储在不同的表中。但它没有被存储。任何人都可以在这个问题上帮助我.. 我尝试了很长时间它不起作用。请帮助我。下面给出了我在其他中使用的其他 java 代码。我想要的是我需要在不同的活动中获取名称和位置,并且我想将它们存储在不同的表中。请帮助我..

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.place);

    t = (EditText) findViewById(R.id.editText1);

    tv = (TextView) findViewById(R.id.textView1);

    View bb = (Button) findViewById(R.id.button1);
    bb.setOnClickListener(this);

}



@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    String place = t.getText().toString();
    ArrayList<NameValuePair> namevalueparams = new ArrayList<NameValuePair>();
    namevalueparams.add(new BasicNameValuePair("place", place));


    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/insert11.php");
        httppost.setEntity(new UrlEncodedFormEntity(namevalueparams));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        iss = entity.getContent();
        Toast.makeText(getBaseContext(), "Congrats! ur added", Toast.LENGTH_LONG).show();



    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Not connected to DataBase", Toast.LENGTH_LONG).show();
    }
}
4

1 回答 1

0

好吧,您在上面的代码中调用了 insert1.php,在下面的代码中调用了 insert11.php。如果第一个有效而第二个无效,则必须与您的两个不同 PHP 中的内容有所不同。

期望不同的代码库有不同的结果:)

于 2013-04-08T20:49:25.130 回答