2012-08-11 43 views
-2

如何爲這樣的圖像製作java代碼?適用於Android應用程序的Java代碼插入數據並將數據上傳到MYSQL

也就是說從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); 
     } 

    } 


} 

謝謝!

+0

那麼...你到底在問什麼?你有什麼嘗試?你做了什麼研究?什麼不起作用? – 0gravity 2012-08-11 04:50:39

+0

嗨,編輯。爲了獲得有用的幫助,通常最好提供一個特定的問題或一組問題,你已經完成的工作來回答他們,並且描述你的障礙。如果您在開始時遇到困難,請嘗試通讀一些Android開發教程。 http://developer.android.com/training/index.html – Carth 2012-08-11 04:53:20

回答

0

完成此操作的一種方法是使用Webservice。你必須創建web服務才能訪問MySQL數據庫。它可以使用PHP,JSP等

我們通過使用數值互聯網連接到web服務來創建。

webservice處理這些數據並返回響應。

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

此鏈接可以幫助你學習如何做到這一點..

+0

你的意思是,SQLite? – 2012-08-11 05:22:05

+0

噢,好吧......謝謝。我應該刪除答案還是刪除整個答案? – 2012-08-11 05:24:31

0

有各種各樣的方式,你可以做到這一點。

一種方法(可能是最簡單)是使用JDBC和合適的JDBC驅動程序,直接從您的Android應用程序訪問數據庫;例如如此問題Using SQL (JDBC) database in Android中所述。 (這假定您瞭解如何使用SQL和JDBC ......但這些都是標準的技術,你需要了解他們,如果你打算做什麼嚴重使用從Java MySQL數據庫。)

另一種方法是可以爲數據庫創建某種包裝服務,但如果您只是想在沒有任何額外的驗證或處理的情況下上傳數據,這對我來說似乎是不必要的複雜。

相關問題