如何使用两个不同的活动将数据放在两个不同的表中

时间:2013-04-08 20:20:23

标签: android http android-layout android-intent http-headers

这里在我的第一个活动的应用程序中,我使用以下代码将数据存储到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();
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,你在上面的代码中调用insert1.php并在下面的代码中插入11.php。如果第一个工作而第二个不工作,则必须与两个不同的PHP内部存在差异。

对于不同的代码库,预计会有不同的结果:)