将数据从MySql Server传输到Android App

时间:2012-01-10 17:23:31

标签: php android mysql database soap

我正在制作一个需要从MySQL数据库访问数据(发送和检索)的Android应用程序。该数据库由我创建的网站提供。我已经创建了数据库。现在我需要应用程序将数据传输到它。有人可以帮帮我吗?

我还附上以下代码。我在应用程序中有3个活动。我发布了Register Activity的代码,它将数据发送到数据库。该数据库位于my3gb.com网站上,该网站提供PHPMyAdmin来改变它。

package com.abc;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class RegActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reg);
    EditText e1 = (EditText) findViewById(R.id.editText1);
    EditText e2 = (EditText) findViewById(R.id.editText2);
    EditText e3 = (EditText) findViewById(R.id.editText3);
    EditText e4 = (EditText) findViewById(R.id.editText4);
    EditText e5 = (EditText) findViewById(R.id.editText5);
    EditText e7 = (EditText) findViewById(R.id.editText7);
    Button b1 = (Button) findViewById(R.id.sub);

    final String username = e1.getText().toString();
    final String phone_no = e2.getText().toString();
    final String email = e3.getText().toString();
    final String bld_grp = e4.getText().toString();
    final String city = e5.getText().toString();
    final String password = e7.getText().toString();



    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            InputStream is = null;
            InputStream is1 = null;
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("username",username));
            nameValuePairs.add(new BasicNameValuePair("phone_no",phone_no));
            nameValuePairs.add(new BasicNameValuePair("email",email));
            nameValuePairs.add(new BasicNameValuePair("bld_grp",bld_grp));
            nameValuePairs.add(new BasicNameValuePair("city",city));
            nameValuePairs.add(new BasicNameValuePair("password",password));
            try
            {     
                 HttpClient httpclient = new DefaultHttpClient();
                 HttpPost httppost = new HttpPost("http://www.prozac.my3gb.com/UserPass.php");
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                 HttpResponse response = httpclient.execute(httppost);
                 HttpEntity entity = response.getEntity();
                 is = entity.getContent();
                 is1 = is;
             }

            catch(Exception e)
            {
                     Log.e("log_tag", "Error in http connection"+e.toString());
            }
            //convert response to string
            String res = null;
            try
            {

                   BufferedReader reader = new BufferedReader(new InputStreamReader(is1,"iso-8859-1"),8);
                   StringBuilder sb = new StringBuilder();
                   sb.append(reader.readLine() + "\n");
                   String line="0";
                   while ((line = reader.readLine()) != null) 
                   {
                                  sb.append(line + "\n");        
                    }
                    is.close();
                    String result=sb.toString();
                    res=result;
              }
            catch(Exception e)
            {
                          Log.e("log_tag", "Error converting result "+e.toString());
            }
            //parsing data
            try
            {

                  JSONArray jArray = new JSONArray(res);
                  int [] deal_id=new int[jArray.length()+1];
                  JSONObject json_data=null;
                  for(int i=0;i<jArray.length();i++)
                  {
                         json_data = jArray.getJSONObject(i);
                  }
            }      
            catch(JSONException ej)
            {
            }
            catch (ParseException ex) 
            {
                        ex.printStackTrace();   
            }


        }
    });

} }

单击“提交”时,应用程序停止响应。

我最早会感谢你的帮助。

2 个答案:

答案 0 :(得分:0)

我建议创建一个连接到数据库的简单REST Web服务,这样您的应用就可以发出简单的GET和POST请求。

我过去曾使用Flask进行简单的网络服务,并强烈推荐。

答案 1 :(得分:0)

这是我遵循的教程,它真的帮助了我。它使用XML,可以很好地与我的MySQL数据库进行通信。还有一些教程可以用JSON做同样的事情。

http://p-xr.com/android-tutorial-how-to-parseread-xml-data-into-android-listview/