2012-03-03 8 views
-2

私は自分のログイン画面をコード化するのに苦労していましたが、とにかく私はいくつかのコードをオンラインで見つけ出し、それを実行しました。これまでのところ動作し、少し変更しました。私の自己をコード化しましたが、私はそれをちょうど理解することができます。RESULT_OKがログインの詳細を確認する際にインテントを追加する方法は?

今、ログインが成功した場合は、インテントを使用します。

コード:ログインの成否が決定される

public class LogIn extends Activity implements OnClickListener { 

Button ok, back, exit; 
TextView result; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Login button clicked 
    ok = (Button) findViewById(R.id.btn_login); 
    ok.setOnClickListener(this); 

    result = (TextView) findViewById(R.id.lbl_result); 

} 

public void postLoginData() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 

    /* login.php returns true if username and password is equal to saranga */ 
    HttpPost httppost = new HttpPost(
      "http://www.sencide.com/blog/login.php"); 

    try { 
     // Add user name and password 
     EditText uname = (EditText) findViewById(R.id.txt_username); 
     String username = uname.getText().toString(); 

     EditText pword = (EditText) findViewById(R.id.txt_password); 
     String password = pword.getText().toString(); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("username", username)); 
     nameValuePairs.add(new BasicNameValuePair("password", password)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     Log.w("SENCIDE", "Execute HTTP Post Request"); 
     HttpResponse response = httpclient.execute(httppost); 

     String str = inputStreamToString(response.getEntity().getContent()) 
       .toString(); 
     Log.w("SENCIDE", str); 

     if (str.toString().equalsIgnoreCase("true")) { 
      Log.w("SENCIDE", "TRUE"); 
      result.setText("Login successful"); 

     } else { 
      Log.w("SENCIDE", "FALSE"); 
      result.setText(str); 
     } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private StringBuilder inputStreamToString(InputStream is) { 
    String line = ""; 
    StringBuilder total = new StringBuilder(); 
    // Wrap a BufferedReader around the InputStream 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    // Read response until the end 
    try { 
     while ((line = rd.readLine()) != null) { 
      total.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // Return full string 
    return total; 
} 

public void onClick(View view) { 
    if (view == ok) { 
     postLoginData(); 
    } 
} 

} 
+0

あなたは何を意味する「意思を使うのか?」あなたはインテントがやると思うことを達成したいと思いますか?あなたが説明していないゴールに対して可能な解決法をあいまいに示唆するのではなく、達成したいことに関してあなたの目標を述べてみてください。 –

+0

ユーザー名とパスワードが受け入れられた場合、インテントが発生します。 – TheBlueCat

答えて

0

このセクションでは、次のとおりです。

if (str.toString().equalsIgnoreCase("true")) { 
     Log.w("SENCIDE", "TRUE"); 
     result.setText("Login successful"); 
     // Do your Intent work here  <------- 
    } else { 
     Log.w("SENCIDE", "FALSE"); 
     result.setText(str); 
    } 
関連する問題