【问题标题】:to insert android data into mysql将android数据插入mysql
【发布时间】:2016-11-19 20:48:52
【问题描述】:

我正在处理注册和登录活动。 当我输入名称、用户名和密码时,信息访问了 android 应用程序。 和 Toast 显示“注册成功”消息。 但是数据没有插入mysql DB。 我该如何解决?请帮帮我。

下面是LoginActivity.java

public class LoginActivity extends Activity {
    EditText ET_NAME,ET_PASS;
    String login_name,login_pass;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_main);
        ET_NAME = (EditText)findViewById(R.id.user_name);
        ET_PASS = (EditText)findViewById(R.id.user_pass);
    }
    public void userReg(View view)
    {
        startActivity(new Intent(this,RegisterActivity.class));
    }
    public void userLogin(View view)
    {
        login_name = ET_NAME.getText().toString();
        login_pass = ET_PASS.getText().toString();
        String method = "login";
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method,login_name,login_pass);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("ID", login_name);
        intent.putExtra("PW", login_pass);
        startActivity(intent);

        finish();
    }
}

这是 BackgroundTask.java

public class BackgroundTask extends AsyncTask<String,Void,String> {
    AlertDialog alertDialog;
    Context ctx;

    BackgroundTask(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(ctx).create();
    }

    @Override
    protected String doInBackground(String... params) {
        String reg_url = "http://35.160.135.119/webapp/register.php";
        String login_url = "http://35.160.135.119/webapp/login.php";
        String method = params[0];
        if (method.equals("register")) {
            String name = params[1];
            String user_name = params[2];
            String user_pass = params[3];
            try {
                URL url = new URL(reg_url)                
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                //httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                        URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                        URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");


                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return "Registration Success...";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (method.equals("login")) {
            String login_name = params[1];
            String login_pass = params[2];
            try {
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" +
                        URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String response = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    response += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return response;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        if (result.equals("Registration Success...")) {
            Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
        } else {
            alertDialog.setMessage(result);
            alertDialog.show();
        }
    }
}

这是Register.java

public class RegisterActivity extends Activity {
    EditText ET_NAME, ET_USER_NAME, ET_USER_PASS;
    String name, user_name, user_pass;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        ET_NAME = (EditText)findViewById(R.id.name);
        ET_USER_NAME = (EditText)findViewById(R.id.new_user_name);
        ET_USER_PASS = (EditText)findViewById(R.id.new_user_pass);
    }
    public void userReg(View view)
    {
        name = ET_NAME.getText().toString();
        user_name = ET_USER_NAME.getText().toString();
        user_pass = ET_USER_PASS.getText().toString();
        String method = "register";
        BackgroundTask backgroundTask = new BackgroundTask(this);
        backgroundTask.execute(method,name, user_name, user_pass);
        finish();

    }

}

这是调试日志。

$ adb shell am start -n "com.example.jina.a1105gmdemo/com.example.jina.a1105gmdemo.LoginActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Connecting to com.example.jina.a1105gmdemo
Connected to the target VM, address: 'localhost:8605', transport: 'socket'
I/System.out: Sending WAIT chunk
W/ActivityThread: Application com.example.jina.a1105gmdemo is waiting for the debugger on port 8100...
I/dalvikvm: Debugger is active
I/System.out: Debugger has connected
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1484)
I/MultiDex: VM with version 1.6.0 does not have multidex support
I/MultiDex: install
I/MultiDex: MultiDexExtractor.load(/data/app/com.example.jina.a1105gmdemo-46.apk, false)
I/MultiDex: Detected that extraction must be performed.
I/MultiDex: Trying to delete old file /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-45.apk.classes2.dex of size 2898496
I/MultiDex: Deleted old file /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-45.apk.classes2.dex
I/MultiDex: Trying to delete old file /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-45.apk.classes2.zip of size 934986
I/MultiDex: Deleted old file /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-45.apk.classes2.zip
I/MultiDex: Extraction is needed for file /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-46.apk.classes2.zip
I/MultiDex: Extracting /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-46.apk.classes2089171779.zip
I/MultiDex: Renaming to /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-46.apk.classes2.zip
I/MultiDex: Extraction success - length /data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-46.apk.classes2.zip: 934986
I/MultiDex: load found 1 secondary dex files
D/dalvikvm: DexOpt: --- BEGIN 'com.example.jina.a1105gmdemo-46.apk.classes2.zip' (bootstrap=0) ---
D/dalvikvm: DexOpt: --- END 'com.example.jina.a1105gmdemo-46.apk.classes2.zip' (success) ---
D/dalvikvm: DEX prep '/data/data/com.example.jina.a1105gmdemo/code_cache/secondary-dexes/com.example.jina.a1105gmdemo-46.apk.classes2.zip': unzip in 66ms, rewrite 778ms
I/MultiDex: install done
I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
              OpenGL ES Shader Compiler Version: E031.24.00.08
              Build Date: 03/21/14 Fri
              Local Branch: AU200+patches_03212014
              Remote Branch: 
              Local Patches: 
              Reconstruct Branch: 
D/OpenGLRenderer: Enabling debug mode 0
D/OpenGLRenderer: GL error from OpenGLRenderer: 0x502
E/OpenGLRenderer:   GL_INVALID_OPERATION
D/OpenGLRenderer: GL error from OpenGLRenderer: 0x502
E/OpenGLRenderer:   GL_INVALID_OPERATION
D/dalvikvm: threadid=1: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
I/System.out: Thread-1263(HTTPLog):isShipBuild true
I/System.out: Thread-1263(HTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
D/dalvikvm: threadid=12: still suspended after undo (sc=1 dc=1)
D/dalvikvm: threadid=1: still suspended after undo (sc=1 dc=1)
Disconnected from the target VM, address: 'localhost:8605', transport: 'socket'

【问题讨论】:

    标签: java android mysql


    【解决方案1】:

    将该代码放在要在异步任务完成后执行的异步任务的后执行中。就像..

      finish();
    

    并检查您的网络服务是否有您在那里收到的数据,并尝试在 logcat 中打印您从服务器脚本获得的结果,而不是静态“成功消息”。

    【讨论】:

    • 谢谢。我检查了我的错误日志并修复了 php 代码。现在可以了。但我无法理解你的第一个建议。把完成();代码进入后执行触发错误..
    • 这是一个很好的做法。因为在异步任务完成执行时会调用 post execute 时...
    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 2013-05-17
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多