【问题标题】:connection between android and php mysql give back null and emptyandroid和php mysql之间的连接返回null和空
【发布时间】:2014-02-11 18:04:31
【问题描述】:

我有这段代码使用 Asynctask 和 json 在 android 和 php 之间创建连接以获取响应,但问题是响应始终为空或 null

我知道这是什么问题: String TAG_NAME 女巫是 php 文件中必须相同的 JSON 节点。

这个字符串总是给出“用户名”并且不接受用户输入。

所以要修复这个错误?? 任何人都可以帮助我吗?我会很感激的。

AndroidPHPConnectionDemo

package pack.coderzheaven;

    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    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;
    import android.widget.TextView;
    import android.widget.Toast;

    public class AndroidPHPConnectionDemo extends Activity {
        Button b;
        EditText et, pass;

        TextView tv;
        HttpPost httppost;
        StringBuffer buffer;
        HttpResponse response;
        HttpClient httpclient;
        List<NameValuePair> nameValuePairs;

        private static String pid;
        private static String Username; 
        private static String Password;

        // Progress Dialog
        private ProgressDialog pDialog;

        // JSON parser class
        JSONParser jsonParser = new JSONParser();

        // single person url
        // ******************************************************************
        // the localhost in the google android emulator = 10.0.2.2
        // the localhost in the genymotion emulator = 10.0.3.2
        // ******************************************************************
        private static final String url_check_login = "http://10.0.3.2/check.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_PERSON = "person";
        // private static final String TAG_PID = "pid";
        private static  String TAG_NAME = "Username";
        private static  String TAG_pass = "password";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);



            b = (Button) findViewById(R.id.Button01);
            et = (EditText) findViewById(R.id.username);



            pass = (EditText) findViewById(R.id.password);
            tv = (TextView) findViewById(R.id.tv);

            b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    // Getting complete person details in background thread
                    new CheckLogin().execute();

                }
            });
        }

        /**
         * Background Async Task to Get complete person details
         * */
        class CheckLogin extends AsyncTask<String, String, String> {

            JSONArray productObj;

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(AndroidPHPConnectionDemo.this);
                pDialog.setMessage("Loading person details. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            /**
             * Getting person details in background thread
             * */

            @Override
            protected String doInBackground(String... arg0) {

                // updating UI from Background Thread

                // Check for success tag

                // Username = et.getText().toString();
                // Log.e("username in create method", " the username is  " +
                // Username);
                int success;
                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("username", Username));

                    // getting person details by making HTTP request
                    // Note that person details url will use GET request
                    JSONObject json = jsonParser.makeHttpRequest(url_check_login,
                            "GET", params);

                    // Log.e("JsonObject", json.toString());
                    // check your log for json response
                    // Log.d("Single person Details", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        // successfully received person details
                        productObj = json.getJSONArray(TAG_PERSON); // JSON Array

    //                  TAG_NAME = et.getText().toString();
        //               Log.e("username in create method", " the username is  " + TAG_NAME);
                    }

                    else {
                        // product with pid not found
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog once got all details
                if (productObj != null) {
                    try {
                        // get first product object from JSON Array
                        JSONObject person = productObj.getJSONObject(0);

                        et.setText(person.getString(Username));

                        //TAG_NAME = Username;

                        pass.setText(person.getString(TAG_pass));

                        Toast.makeText(
                                getBaseContext(),
                                et.getText().toString() + pass.getText().toString(),
                                Toast.LENGTH_LONG).show();

                        Log.e("success in login", "SUCCESS IN LOGIN");

                    } catch (Exception e) {

                        e.printStackTrace();
                    }

                }
                Log.e("after the post execute", " THE USERNAME IS  " + Username);

                pDialog.dismiss();
            }
        }

}

【问题讨论】:

    标签: php android android-asynctask


    【解决方案1】:

    试试这个并观察你的代码中的验证,然后告诉我发生了什么。

    import java.util.ArrayList;
        import java.util.List;
    
        import org.apache.http.HttpResponse;
        import org.apache.http.NameValuePair;
        import org.apache.http.client.HttpClient;
        import org.apache.http.client.methods.HttpPost;
        import org.apache.http.message.BasicNameValuePair;
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;
    
        import android.app.Activity;
        import android.app.ProgressDialog;
        import android.os.AsyncTask;
        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;
        import android.widget.TextView;
        import android.widget.Toast;
    
        public class AndroidPHPConnectionDemo extends Activity {
            Button b;
            EditText et, pass;
    
            TextView tv;
            HttpPost httppost;
            StringBuffer buffer;
            HttpResponse response;
            HttpClient httpclient;
            List<NameValuePair> nameValuePairs;
    
            private static String pid;
            private static String Username; 
            private static String Password;
    
            // Progress Dialog
            private ProgressDialog pDialog;
    
            // JSON parser class
            JSONParser jsonParser = new JSONParser();
    
            // single person url
            // ******************************************************************
            // the localhost in the google android emulator = 10.0.2.2
            // the localhost in the genymotion emulator = 10.0.3.2
            // ******************************************************************
            private static final String url_check_login = "http://10.0.3.2/check.php";
    
            // JSON Node names
            private static final String TAG_SUCCESS = "success";
            private static final String TAG_PERSON = "person";
            // private static final String TAG_PID = "pid";
            private static  String TAG_NAME = "Username";
            private static  String TAG_pass = "password";
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
    
    
    
                b = (Button) findViewById(R.id.Button01);
                et = (EditText) findViewById(R.id.username);
    
    
    
                pass = (EditText) findViewById(R.id.password);
                tv = (TextView) findViewById(R.id.tv);
    
                b.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
    
                        // Getting complete person details in background thread
                        new CheckLogin().execute();
    
                    }
                });
            }
    
            /**
             * Background Async Task to Get complete person details
             * */
            class CheckLogin extends AsyncTask<String, String, String> {
    
                JSONArray productObj;
    
                /**
                 * Before starting background thread Show Progress Dialog
                 * */
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    pDialog = new ProgressDialog(AndroidPHPConnectionDemo.this);
                    pDialog.setMessage("Loading person details. Please wait...");
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                    pDialog.show();
                }
    
                /**
                 * Getting person details in background thread
                 * */
    
                @Override
                protected String doInBackground(String... arg0) {
    
                    // updating UI from Background Thread
    
                    // Check for success tag
    
                    // Username = et.getText().toString();
                    // Log.e("username in create method", " the username is  " +
                    // Username);
    
    
    
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("username", Username));
    
                        // getting person details by making HTTP request
                        // Note that person details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(url_check_login,
                                "GET", params);
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
    
                    return json.toString();
                }
    
                /**
                 * After completing background task Dismiss the progress dialog
                 * **/
                protected void onPostExecute(String json) {
                    // dismiss the dialog once got all details
    int success;
    success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {
                            // successfully received person details
    
                            productObj = json.getJSONArray(TAG_PERSON); // JSON Array
    
        //                  TAG_NAME = et.getText().toString();
            //               Log.e("username in create method", " the username is  " + TAG_NAME);
                        }
                    if (productObj != null) {
                        try {
                            // get first product object from JSON Array
                            JSONObject person = productObj.getJSONObject(0);
    
                            et.setText(person.getString(Username));
    
                            //TAG_NAME = Username;
    
                            pass.setText(person.getString(TAG_pass));
    
                            Toast.makeText(
                                    getBaseContext(),
                                    et.getText().toString() + pass.getText().toString(),
                                    Toast.LENGTH_LONG).show();
    
                            Log.e("success in login", "SUCCESS IN LOGIN");
    
                        } catch (Exception e) {
    
                            e.printStackTrace();
                        }
    
                    }
                    Log.e("after the post execute", " THE USERNAME IS  " + Username);
    
                    pDialog.dismiss();
                }
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2020-08-28
      相关资源
      最近更新 更多