将数据从 MySql Server 传输到 Android 应用程序

发布于 2024-12-26 03:33:42 字数 4874 浏览 0 评论 0原文

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

我还附上了下面的代码。我的应用程序中有 3 个活动。我发布了将数据发送到数据库的注册活动的代码。该数据库位于网站 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();   
            }


        }
    });

} 。

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

我希望尽快获得帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2025-01-02 03:33:42

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

我过去曾使用 Flask 来实现简单的 Web 服务,并且也强烈推荐它。

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.

油焖大侠 2025-01-02 03:33:42

这是我遵循的教程,它对我很有帮助。它使用 XML,可以很好地与我的 MySQL 数据库进行通信。还有一些教程可用于使用 JSON 执行相同的操作。

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文