0

我是 android 应用程序开发的新手。我做了一个编码,用于将数据存储在 android.it 的 wamp 服务器数据库中。它运行良好。但我没有从 PHP 到 android.我收到成功消息。我不知道我犯了什么错误。这是我的编码 .java 文件

package com.example.loga;


import android.os.Bundle;

import android.app.Activity;
import android.app.AlertDialog;

import android.view.Menu;
import android.widget.Button;


import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;



import android.app.ProgressDialog;


import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.EditText;


public class MainActivity1 extends Activity {

     private ProgressDialog pDialog;

     JSONParser jsonParser = new JSONParser();
        EditText inputuserid;
        EditText inputserverid;

        private static String url_register = "http://10.0.2.2/android_connect/register.php";
        // JSON Node names
        private static final String TAG_SUCCESS = "success";





    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity1);
         // Edit Text
        inputuserid = (EditText) findViewById(R.id.editText1);
        inputserverid= (EditText) findViewById(R.id.editText2);
        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view) {
                // creating new product in background thread
                new add().execute();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity1, menu);
        return true;
    }
    class add extends AsyncTask<String, String,String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity1.this);
            pDialog.setMessage("your Registration is processing..wait for few sec..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String userid = inputuserid.getText().toString();
            String serverid = inputserverid.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("userid",userid));
            params.add(new BasicNameValuePair("serverid", serverid));


            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_register,
                    "POST", params);
            // check log cat for response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    AlertDialog alertDialog;
                    alertDialog = new AlertDialog.Builder(MainActivity1.this).create();
                    alertDialog.setTitle("password verification..");
                    alertDialog.setMessage("success.!!");
                    alertDialog.show();

                    finish();
                } else {
                    // failed to create product
                }
             }
            catch (Exception e) {
                e.printStackTrace();
            }



            return null;

        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }

}

我的 PHP 编码是

<?php
 $response = array();
 // check for required fields
if (isset($_POST['userid']) && isset($_POST['serverid']))
{

    $userid = $_POST['userid'];
    $serverid= $_POST['serverid'];
       // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO opass(User_ID, Server_ID ) VALUES('$userid', '$serverid')");

    // check if row inserted or not
    if ($result) 
{
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = " Registered successfully";

        // echoing JSON response
        echo json_encode($response);
    }
 else 
{
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
}
 else 
{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

?>

我想在数据库中存储数据后在模拟器中显示一个消息框。帮助我。谢谢:)

4

1 回答 1

0

无法在 doInBackground(String... args) 方法中进行 UI 修改。将创建和显示对话框的逻辑移动到 onPostExecute(String file_url) 方法中。

从 doInBackground() 返回一个布尔状态并检查 onPostExecute() 中的状态并相应地显示对话框。

于 2013-04-22T04:01:34.527 回答