2013-07-31 20 views
0

我使用下面做出一個PHP腳本調用登錄用戶:onPostExecute不讀導致HTTPPost

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button loginButton = (Button) findViewById(R.id.loginButton); 

     loginButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       new loginClass().execute(); 
      } 
     }); 
    } 

    class loginClass extends AsyncTask<String, String, Void> { 
     private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this); 
     InputStream is = null ; 
     String result = ""; 
     protected void onPreExecute() { 
      progressDialog.setMessage("Logging you in..."); 
      progressDialog.show(); 
      progressDialog.setCancelable(false); 
      progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
       public void onCancel(DialogInterface arg0) { 
        loginClass.this.cancel(true); 
       } 
      }); 
     } 
     @Override 
     protected Void doInBackground(String... params) { 
      try { 
       HttpPost httpPost = new HttpPost("http://www.example.com/login.php"); 
       HttpParams httpParameters = new BasicHttpParams(); 
       HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); 
       HttpConnectionParams.setSoTimeout(httpParameters, 3000); 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       EditText uname = (EditText)findViewById(R.id.usernameField); 
       String username = uname.getText().toString(); 
       EditText pword = (EditText)findViewById(R.id.passwordField); 
       String password = pword.getText().toString(); 
       nameValuePairs.add(new BasicNameValuePair("username", username)); 
       nameValuePairs.add(new BasicNameValuePair("password", password)); 
       HttpClient httpClient = new DefaultHttpClient(httpParameters); 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
       Log.e("log_tag", "Open Connection"); 


      } catch(ConnectTimeoutException e){ 
       Log.e("Timeout Exception: ", e.toString()); 
       result = null; 
      } catch(SocketTimeoutException ste){ 
       Log.e("Timeout Exception: ", ste.toString()); 
       result = null; 
      } catch (Exception e) { 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
       result = null; 
      } 

      try { 
       BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = ""; 
       while((line=br.readLine())!=null){ 
        sb.append(line+"\n"); 
       } 
       is.close(); 
       result=sb.toString(); 
       Log.e("log_tag", "Result: "+result); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error converting result "+e.toString()); 
       result = null; 
      } 
      return null; 

     } 

     protected void onPostExecute(Void v) { 
      Log.e("log_tag", "Execute Started - Result: "+result); 
      if (result == null){ 
       this.progressDialog.dismiss(); 
       AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); 
       alertDialog.setTitle("Connection Error"); 
       alertDialog.setMessage("There was a problem with your connection."); 
       alertDialog.setButton("Exit", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         finish(); 
        } 
       }); 
       alertDialog.setButton2("Try Again", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         new loginClass().execute(); 

        } 
       }); 
       alertDialog.show(); 
      }else if("0".equals(result.toString())){ 
       this.progressDialog.dismiss(); 
       Toast.makeText(MainActivity.this, "Invalid Username/Password. Please try again.", Toast.LENGTH_SHORT).show(); 
      }else if("1".equals(result.toString())){ 
       this.progressDialog.dismiss(); 
       Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show(); 
      }else{ 
       this.progressDialog.dismiss(); 
       Toast.makeText(MainActivity.this, "Something Else Happened", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    } 


} 

在logcat的我顯示了Execute Started - Result: 0,但它是不是趕上結果在if語句中,所以last else語句正在觸發。 "0".equals(result.toString())是處理結果的正確方法嗎?

回答

1

使用String.contains()方法。

嘗試result.toString().contains("0")result.toString().contains("1") 這應該起作用。

我建議將響應作爲JSON對象而不是普通HTTP響應進行檢索。

+0

Ahhaa!這工作。非常感謝你。我打算爲此使用JSON,但是我所需要的只是從此登錄腳本返回1或0。謝謝! –

+0

我很高興能幫助傑夫!我發現你只需要0或1,這就是爲什麼我推薦'String.contains()'方法而不是'JSON對象'。否則,'JSON'將是一個很好的解決方案。祝你好運! :) – ARMAGEDDON