2010-12-20 2 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

大文字と小文字は区別されますか?大文字と小文字が区別されない文字列を比較する場合は、.equalsIgnoreCase()を使用する必要があります。また、これを踏んで、デバッグしてtryLogin(mUsername, mPassword)が期待値を返すことを確認しましたか?

+0

コードスニペットを追加できますか? –

+0

if(Re.equals( "GEEK")){...}続きを読むHere-> http://developer.android.com/reference/java/lang/String.html – ninjasense

1

コードに関する重要な点の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

私は何をすべきですか? –

+0

私の答えに上記の例を見てください。 – dstefanox

関連する問題