简体   繁体   中英

Transfer of data from MySql Server to Android App

I am making an Android app that needs to access data (send as well as retrieve) from a MySQL database. This database is provided by the website I have created. I've created the database. Now I need the app to transfer data to it. Could anyone please help me out ?

I'm also attaching the code below. I have 3 Activities in the App. I'm posting the code of the Register Activity which sends the data to the database. The database is on the website my3gb.com which provides PHPMyAdmin to alter it.

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();   
            }


        }
    });

} }

The application stops responding when I click submit.

I would appreciate help at the earliest.

I would recommend creating a simple REST web service that connects to your database, so your app can make simple GET and POST requests.

I've used Flask in the past for simple web services and highly recommend that too.

Here's the tutorial I followed which really helped me. It uses XML which worked well for communicating with my MySQL database. There are also tutorials out there for doing the same thing with JSON.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM