2014-02-11 23 views
-1

我有这样的代码,创建使用的AsyncTask和JSON找回响应的Android和PHP之间的连接,但问题是,反应总是空或空连接Android和PHP MySQL的回馈空和空

我知道它是什么的问题是: 的字符串TAG_NAME魔女nodethat必须在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(); 
      } 
     } 

} 

回答

0

试试这个并观察代码中的证明,让我现在发生了什么事。

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(); 
      } 
     } 

}