通过Android SDK连接MySQL数据库

时间:2012-08-19 10:43:34

标签: java android mysql

任何人都可以告诉我在哪里可以找到一个简单的教程,该教程展示如何制作连接外部MySQL数据库的Android应用程序,并报告一些数据?

我在互联网上找到的教程并不准确 - 它们不起作用或代码不完整。

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class main extends Activity {
    /** Called when the activity is first created. */
    Button login;
    String name = "", pass = "";
    EditText username, password;
    TextView tv;
    byte[] data;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    InputStream inputStream;
    SharedPreferences app_preferences;
    List<NameValuePair> nameValuePairs;
    CheckBox check;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        check = (CheckBox) findViewById(R.id.check);

        String Str_user = app_preferences.getString("username", "0");
        String Str_pass = app_preferences.getString("password", "0");
        String Str_check = app_preferences.getString("checked", "no");
        if (Str_check.equals("yes")) {
            username.setText(Str_user);
            password.setText(Str_pass);
            check.setChecked(true);
        }

        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                name = username.getText().toString();
                pass = password.getText().toString();
                String Str_check2 = app_preferences.getString("checked", "no");
                if (Str_check2.equals("yes")) {
                    SharedPreferences.Editor editor = app_preferences.edit();
                    editor.putString("username", name);
                    editor.putString("password", pass);
                    editor.commit();
                }
                if (name.equals("") || pass.equals("")) {
                    Toast.makeText(main.this, "Blank Field..Please Enter",
                            Toast.LENGTH_LONG).show();
                } else {
                    try {
                        httpclient = new DefaultHttpClient();
                        httppost = new HttpPost(
                                "http://www.****.com/android/check.php");

                        // Add your data
                        nameValuePairs = new ArrayList<NameValuePair>(2);
                        nameValuePairs.add(new BasicNameValuePair("UserEmail",
                                name.trim()));
                        nameValuePairs.add(new BasicNameValuePair("Password",
                                pass.trim()));
                        httppost.setEntity(new UrlEncodedFormEntity(
                                nameValuePairs));

                        // Execute HTTP Post Request
                        response = httpclient.execute(httppost);
                        inputStream = response.getEntity().getContent();

                        data = new byte[256];

                        buffer = new StringBuffer();
                        int len = 0;
                        while (-1 != (len = inputStream.read(data))) {
                            buffer.append(new String(data, 0, len));
                        }

                        inputStream.close();
                    } catch (Exception e) {
                        Toast.makeText(main.this, "error" + e.toString(),
                                Toast.LENGTH_LONG).show();
                    }

                    if (buffer.charAt(0) == 'Y') {
                        Toast.makeText(main.this, "login successfull",
                                Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(main.this,
                                "Invalid Username or password",
                                Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
        check.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on clicks, depending on whether it's now
                // checked
                SharedPreferences.Editor editor = app_preferences.edit();
                if (((CheckBox) v).isChecked()) {

                    editor.putString("checked", "yes");
                    editor.commit();
                } else {
                    editor.putString("checked", "no");
                    editor.commit();
                }
            }
        });
    }

    public void Move_to_next() {
        // startActivity(new Intent(this, zzz.class));
    }
}

1 个答案:

答案 0 :(得分:1)

我无法轻易找到一个完整描述的例子。但是我会在哪里开始。

  1. 查看内容提供商,以管理您的应用数据并对其进行访问并将其存储以供本地使用。下面的链接提供了有关内容提供商如何工作的广泛解释。虽然你不必使用它。
  2. http://www.satyakomatineni.com/akc/display?url=DisplayNoteIMPURL&reportId=2882&ownerUserId=satya

    android内容提供程序示例也显示了这一点。

    1. 在您家中的服务器上,为您的应用提供一个休息的完整服务层,以便从中请求信息。而不是尝试直接访问数据库自己。在某种程度上讨论了如何做(不具体如何做)https://groups.google.com/forum/?fromgroups#!topic/android-developers/rzV9tYpQZ5Y%5B1-25%5D
    2. 害怕我没有编码的例子。