Android App的Java代码,用于插入和上传数据到MYSQL

时间:2012-08-11 04:48:01

标签: java android mysql post

你会如何为这样的图像制作java代码?

这是来自Android模拟器。我正在尝试将后期数据信息输入每个插槽并将信息上传到MySQL数据库。

这是到目前为止的代码:

public class Registration extends Activity {

    TextView Taccount,Tpassword,Tfirst,Tlast,Temail,Tlocation,Tdescription;
    EditText Eaccount,Epassword,Efirst,Elast,Eemail,Elocation,Edescription;
    Button btnCreate;
    String page="";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cappuccino);
        btnCreate = (Button) findViewById(R.id.btnGen);
        Taccount = (TextView) findViewById(R.id.txtAccountID);
        Eaccount = (EditText) findViewById(R.id.editAccountID);
        Tpassword = (TextView) findViewById(R.id.txtPassword);
        Epassword = (EditText) findViewById(R.id.editPassword);
        Tfirst = (TextView) findViewById(R.id.txtFirst);
        Efirst = (EditText) findViewById(R.id.editFirst);
        Tlast = (TextView) findViewById(R.id.txtLast);
        Elast = (EditText) findViewById(R.id.editLast);
        Temail = (TextView) findViewById(R.id.txtEmail);
        Eemail = (EditText) findViewById(R.id.editEmail);
        Tlocation = (TextView) findViewById(R.id.txtLocation);
        Elocation = (EditText) findViewById(R.id.editLocation);
        Tdescription = (TextView) findViewById(R.id.txtDescription);
        Edescription = (EditText) findViewById(R.id.editDescription);

        btnCreate.setOnClickListener(new Button.OnClickListener()
        {
            public void onClick(View v)
            {
                examineJSONFile();
            }
        });
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_cappuccino, menu);
        return true;
    }

    void examineJSONFile()
    {
        try
        {
            JSONObject object=new JSONObject();
            object.put("Account ID", Eaccount.getText());
            object.put("Password", Epassword.getText());
            object.put("First Name", Efirst.getText());
            object.put("Last Name", Elast.getText());
            object.put("Email", Eemail.getText());
            object.put("Location", Elocation.getText());
            object.put("Description", Edescription.getText());
            String str=object.toString();
            executeHttpPost(str);
            Log.i("JsonString :", str);
            Toast.makeText(this, "Json Objects are : " + str,Toast.LENGTH_LONG).show();


        }
        catch (Exception je)
        {

        }
    }

    public  void executeHttpPost(String string) throws Exception 
    {
        //This method  for HttpConnection  
        try 
        {
            HttpClient client = new DefaultHttpClient();

            HttpPost request = new HttpPost("http://localhost:8080/example/database/Cappuccino.sql");

            List<NameValuePair> value=new ArrayList<NameValuePair>();

            value.add(new BasicNameValuePair("Account ID",string));

            UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);

            request.setEntity(entity);

            client.execute(request);

           System.out.println("after sending :"+request.toString());

        } 
     catch(Exception e)     {System.out.println("Exp="+e);
        }

    }


}

提前致谢!

2 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是使用Webservice。您必须创建webservice才能访问MySQL数据库。它可以使用php,jsp等创建。

我们使用互联网连接将值传递给webservice。

webservice处理此数据并返回响应。

http://www.codeproject.com/Articles/112381/Step-by-Step-Method-to-Access-Webservice-from-Andr

此链接可以帮助您了解如何执行此操作..

答案 1 :(得分:0)

您可以通过多种方式实现此目标。

一种方法(可能是最简单的)是使用JDBC和合适的JDBC驱动程序直接从Android应用程序访问数据库;例如如本问题Using SQL (JDBC) database in Android中所述。 (这假设您了解如何使用SQL和JDBC ......但这些是标准技术,如果您要使用Java中的MySQL数据库执行任何严肃的操作,则需要了解它们。)

另一种方法是为数据库创建某种包装服务,但如果你只是想上传数据而不需要任何额外的验证或处理,这对我来说似乎不必要的复杂。