2015-06-09 64 views
-2

我的登錄代碼在genymotion中運行得非常好,它不會在android studio上運行。在genymotion中,他在數據庫中找到了用戶名和密碼,我可以在android studio中登錄,但一直說php文件中存在語法錯誤。我已經複製粘貼相同的代碼的一切,它仍然給我同樣的錯誤。Genymotion中的代碼在Android Studio中不起作用

package com.example.login2; 


import java.util.ArrayList; 


    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class login2 extends Activity { 
EditText un,pw; 
TextView error; 
Button ok; 
protected String macAddress; 
protected String IPAddress; 
protected String version; 
/** Called when the activity is first created. */ 

// public void LoggedIn(View view) { 
// Do something in response to button 
//  Intent intent = new Intent(this, Logout.class); 
//  startActivity(intent); 
// } 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    un=(EditText)findViewById(R.id.et_un); 
    pw=(EditText)findViewById(R.id.et_pw); 
    ok=(Button)findViewById(R.id.btn_login); 
    error=(TextView)findViewById(R.id.tv_error); 
    macAddress = CustomHttpClient.getMacAddress(getBaseContext()); 
    IPAddress = CustomHttpClient.getIP(getBaseContext()); 
    version = CustomHttpClient.getAndroidVersion(); 




    ok.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("username", un.getText().toString())); 
      postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
      postParameters.add(new BasicNameValuePair("MacAddress",macAddress.toString())); 
      postParameters.add(new BasicNameValuePair("IPAddress",IPAddress.toString())); 
      postParameters.add(new BasicNameValuePair("Version",version.toString())); 
      //String valid = "1"; 
      String response = null; 
      try { 
       response = CustomHttpClient.executeHttpPost("http://192.168.1.7/thesis/check.php", postParameters); 
       String res=response.toString(); 
       // res = res.trim(); 
       res= res.replaceAll("\\s+",""); 
       //error.setText(res); 

       if(res.equals("1")){ 
        error.setTextColor(Color.GREEN); 
        error.setText("Correct Username or Password"); 
        Toast.makeText(getApplicationContext(), "You Logged in ", Toast.LENGTH_LONG).show(); 
        //Intent startMain = new Intent(Intent.ACTION_MAIN); 
        //startMain.addCategory(Intent.CATEGORY_HOME); 
        //startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        // startActivity(startMain); 
        Intent intent = new Intent(); 
        intent.setClass(login2.this, Logout.class); 
        startActivity(intent); 

       } 
       else{ 
        error.setText("Sorry!! Incorrect Username or Password"); 
        Toast.makeText(getApplicationContext(), "Sorry, wrong Username/Password", Toast.LENGTH_LONG).show(); 
       } 
      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show(); 


      } 

     } 
    }); 
} 
} 

回答

0

首先,你應該採取的Android看看關於異步任務:

Android HTTP Request AsyncTask

http://loopj.com/android-async-http/

而且,正如你說,「而在Android Studio中口口聲聲說有在PHP文件中的語法錯誤「,該語法錯誤是在你的PHP文件,你只是閱讀HTTP響應消息。

此外,我建議您閱讀有關webservices及其安全實現。要求一個「1或0」的數字並不太安全,並對該響應進行檢查。

Secure Web Services: REST over HTTPS vs SOAP + WS-Security. Which is better?

https://developer.android.com/training/articles/security-ssl.html

相關問題