-2

このような画像の 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);
        }

    }


}

前もって感謝します!

4

2 に答える 2

0

これを行うには、さまざまな方法があります。

1 つの方法 (おそらく最も単純な方法) は、JDBC と適切な JDBC ドライバーを使用して、Android アプリからデータベースに直接アクセスすることです。たとえば、この質問Using SQL (JDBC) database in Android で説明されているように。(これは、SQL と JDBC の使用方法を理解していることを前提としています。しかし、これらは標準的なテクノロジであり、Java から MySQL データベースを使用して何か重大なことを行う場合は、それらを理解する必要があります。)

もう 1 つのアプローチは、データベース用の何らかのラッパー サービスを作成することですが、追加の検証や処理を行わずにデータをアップロードするだけの場合、これは不必要に複雑に思えます。

于 2012-08-11T05:33:01.457 に答える
0

これを実現する 1 つの方法は、Web サービスを使用することです。MySQL データベースにアクセスするには、Web サービスを作成する必要があります。php、jspなどで作成できます。

インターネット接続を使用して値を webservice に渡します。

webservice はこのデータを処理し、応答を返します。

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

このリンクは、これを行う方法を学ぶのに役立ちます..

于 2012-08-11T04:54:00.180 に答える