2010-12-20 69 views
1

我使用下面的代碼從服務器獲取數據,如果從服務器值GEEK那麼它會自動裝載到下一節課,但新視圖沒有加載新的屏幕。你能說出什麼問題嗎?從服務器獲取數據,並加載使用Android

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

     login = (Button) findViewById(R.id.login); 
     username = (EditText) findViewById(R.id.username); 
     password = (EditText) findViewById(R.id.password); 

     login.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
String Re; 
       String mUsername = username.getText().toString(); 
       String mPassword = password.getText().toString(); 

       Re=tryLogin(mUsername, mPassword); 

       if(Re=="GEEK") 
       { 
        Intent i = new Intent(); 
        i.setClassName(v.getContext(),"com.httplogin.MainScreen"); 
        startActivity(i); 

       } 
      } 
     }); 
    } 

    protected String tryLogin(String mUsername, String mPassword) 
    {   
     HttpURLConnection connection; 
     OutputStreamWriter request = null; 

      URL url = null; 
      String response = null;   
      String parameters = "username="+mUsername+"&password="+mPassword; 

      try 
      { 
       url = new URL("http://serverspace/script.php"); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestMethod("POST");  

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.write(parameters); 
       request.flush(); 
       request.close();    
       String line = "";    
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 
       StringBuilder sb = new StringBuilder(); 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       // Response from server after login process will be stored in response variable.     
       response = sb.toString(); 

       isr.close(); 
       reader.close(); 


      } 
      catch(IOException e) 
      { 
       Toast.makeText(this,e.toString(),0).show(); 
      } 
      return response; 
    } 
} 

回答

2

請參閱本代碼..............,使用微調功能,因爲它消除了字符串中的空間..

public class HttpLogin extends Activity { 
    /** Called when the activity is first created. */ 
    private Button login; 
    private EditText username, password; 

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

     login = (Button) findViewById(R.id.login); 
     username = (EditText) findViewById(R.id.username); 
     password = (EditText) findViewById(R.id.password); 

     login.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      String Re; 
       String mUsername = username.getText().toString(); 
       String mPassword = password.getText().toString(); 

       Re=tryLogin(mUsername, mPassword); 
       Log.d(" Check ","Here"); 
       Log.d("Re",Re); 
       String temp_check=Re.trim(); 
       if(temp_check.equals("GEEK")) 
       { 
        Intent i = new Intent(); 
        i.setClassName(v.getContext(),"com.httplogin.MainScreen"); 
        startActivity(i); 

       } 
       else 
       { 
       //Toast.makeText(HttpLogin.this,"MAX Returned",0).show(); 
       displayAlert(Re); 
//     Intent i = new Intent(); 
//      i.setClassName(v.getContext(),"com.san.MainScreen"); 
//      startActivity(i); 
       } 
      } 
     }); 
    } 

    protected String tryLogin(String mUsername, String mPassword) 
    {   
     Log.d(" TryLoginCheck ","Here"); 
     HttpURLConnection connection; 
     OutputStreamWriter request = null; 

      URL url = null; 
      String response = null; 
      String temp=null; 
      String parameters = "username="+mUsername+"&password="+mPassword; 
      System.out.println("UserName"+mUsername+"\n"+"password"+mPassword); 
      Log.d("Parameters",parameters); 
      try 
      { 

       url = new URL("http://serverspace/script.php"); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestMethod("POST");  

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.write(parameters); 
       request.flush(); 
       request.close();    
       String line = "";    
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 
       StringBuilder sb = new StringBuilder(); 
       while ((line = reader.readLine()) != null) 
       { 

        sb.append(line + "\n"); 
       } 
       temp=sb.toString(); 
       Log.d("Temp",temp); 
       // Response from server after login process will be stored in response variable.     
       response = sb.toString(); 
       Log.d("Response",response); 
       Log.d("Sb Value",sb.toString()); 
       isr.close(); 
       reader.close(); 


      } 
      catch(IOException e) 
      { 
       Toast.makeText(this,e.toString(),0).show(); 
      } 
      // Log.d("Response",response); 
      return response; 
    } 
    public void displayAlert(String Re) 
    { 
    new AlertDialog.Builder(this).setMessage(Re) 
     .setTitle("Returned Value") 
     .setCancelable(true) 
     .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton){ 
      finish(); 
      } 
      }) 
     .show(); 
    } 
} 
2

是否區分大小寫?在比較大小寫無關緊要的字符串和.equals()時,您應該使用.equalsIgnoreCase()。你是否也已經通過了這個調試,以確保tryLogin(mUsername, mPassword)正在返回預期值?

+0

可以添加代碼片段嗎? – 2010-12-20 07:04:50

+0

如果(Re.equals( 「奇客」)){...}瞭解更多當前位置 - > http://developer.android.com/reference/java/lang/String.html – ninjasense 2010-12-20 07:07:16

1

一個需要注意你的代碼中的重要的事情是,你不應該從UI線程中運行較長時間的操作,例如(在你的情況tryLogin()調用)遠程服務器訪問。這種編程將導致您的應用程序中的ANR。有關此主題的更多信息,請閱讀this article

簡而言之,而不是從的onCreate調用tryLogin,創建異步任務

new LoginTask().execute(mUsername, mPassword); 

登錄任務應該被定義是這樣的:

private class LoginTask extends AsyncTask<string, void,="" String=""> { 
    protected String doInBackground(String... login) { 
     return tryLogin((login[0],login[1]); 
    } 

    protected void onPostExecute(String result) { 
     if(result.equalsIgnoreCase("GEEK")) 
     { 
      Intent i = new Intent(); 
      i.setClassName(v.getContext(),"com.httplogin.MainScreen"); 
      startActivity(i); 
     } 
    } 
} 
+0

我應該怎麼辦???? – 2010-12-20 09:07:32

+0

查看我的答案上面添加的示例。 – dstefanox 2010-12-20 10:47:00