2013-09-25 25 views
1

我在我的應用程序中有以下類,我正在向遠程服務器發送用戶名和密碼,並在服務器端發送匹配值併發送響應。一切工作都很好。我想問如何在登錄成功消息後開始新的活動。消息消失時,我希望新的活動開始。 QnActivity是我想要開始的活動,LoActivity是我目前的活動。我已經嘗試了很多,但沒有成功。我還添加了如何在以下代碼中成功登錄時開始新活動?

startActivity(new Intent(LoActivity.this, QnActivity.class)); 

public void Move_to_next()方法但它不工作。

的Java代碼 -

public class LoActivity extends Activity { 

     Intent i; 
     Button signin; 
     TextView error; 
     CheckBox check; 
     String name="",pass=""; 
     byte[] data; 
     HttpPost httppost; 
     StringBuffer buffer; 
     HttpResponse response; 
     HttpClient httpclient; 
     InputStream inputStream; 
     SharedPreferences app_preferences ; 
     List<NameValuePair> nameValuePairs; 
     EditText editTextId, editTextP; 

     @Override 
     public void onCreate (Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.login); 
      signin = (Button) findViewById (R.id.signin); 
      editTextId = (EditText) findViewById (R.id.editTextId); 
      editTextP = (EditText) findViewById (R.id.editTextP); 
      app_preferences = PreferenceManager.getDefaultSharedPreferences(this); 
      check = (CheckBox) findViewById(R.id.check); 
      String Str_user = app_preferences.getString("username","0"); 
      String Str_pass = app_preferences.getString("password", "0"); 
      String Str_check = app_preferences.getString("checked", "no"); 
      if(Str_check.equals("yes")) 
      { 
       editTextId.setText(Str_user); 
       editTextP.setText(Str_pass); 
       check.setChecked(true); 
      } 

      signin.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        name = editTextId.getText().toString(); 
        pass = editTextP.getText().toString(); 
        String Str_check2 = app_preferences.getString("checked", "no"); 
        if(Str_check2.equals("yes")) 
        { 
         SharedPreferences.Editor editor = app_preferences.edit(); 
         editor.putString("username", name); 
         editor.putString("password", pass); 
         editor.commit(); 
        } 
        if(name.equals("") || pass.equals("")) 
        { 
         Toast.makeText(Lo.this, "Blank Field..Please Enter", Toast.LENGTH_SHORT).show(); 
        } 
        else 
        { 

        try { 
         httpclient = new DefaultHttpClient(); 
         httppost = new HttpPost("http://abc.com/register.php"); 
         // Add your data 
         nameValuePairs = new ArrayList<NameValuePair>(2); 
         nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim())); 
         nameValuePairs.add(new BasicNameValuePair("Password", pass.trim())); 
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
         // Execute HTTP Post Request 
         response = httpclient.execute(httppost); 
         inputStream = response.getEntity().getContent(); 

         data = new byte[256]; 

         buffer = new StringBuffer(); 
         int len = 0; 
         while (-1 != (len = inputStream.read(data))) 
         { 
          buffer.append(new String(data, 0, len)); 
         } 

         inputStream.close(); 
        } 

        catch (Exception e) 
        { 
         Toast.makeText(LoActivity.this, "error"+e.toString(), Toast.LENGTH_SHORT).show(); 
        } 
        if(buffer.charAt(0)=='Y') 
        { 
         Toast.makeText(LoActivity.this, "login successfull", Toast.LENGTH_SHORT).show(); 
        } 
        else 
        { 
         Toast.makeText(LoActivity.this, "Invalid Username or password", Toast.LENGTH_SHORT).show(); 
        } 
        } 
       } 
      }); 

     check.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       // Perform action on clicks, depending on whether it's now checked 
       SharedPreferences.Editor editor = app_preferences.edit(); 
       if (((CheckBox) v).isChecked()) 
       { 
        editor.putString("checked", "yes"); 
        editor.commit(); 
       } 
       else 
       { 
        editor.putString("checked", "no"); 
        editor.commit(); 
       } 
     } 
     }); 
     } 
     public void Move_to_next() 
     { 
      startActivity(new Intent(LoActivity.this, QnActivity.class)); 

     } 
    } 
+1

「不成功」是什麼意思?應用程序是否會崩潰,什麼都不發生,或發生其他事情? 「不起作用」不是對問題的一個好的描述。 –

+0

我不知道如何使用Asyncthask。 codeMagic提供的鏈接很好,但asynctask對我來說是新的。所以我不知道如何合併我的代碼與asynctask代碼。任何人都可以幫助我。 –

+0

@ Tanis.7x成功登錄後,其他人表示Move_to_next()從未調用過。和其他人已經知道的logcat在主線程上顯示過多的工作。但我不知道如何在我的代碼中使用asynctask。 –

回答

2

您正在運行的網絡realted在UI線程上運行。使用線程或的AsyncTask

response = httpclient.execute(httppost); 

你會得到NetworkOnMainThreadException蜂窩後,如果你的UI線程

上運行網絡相關operaion並確保你在你的代碼中調用Move_to_next()

要調用AsyncTask在UI線程

new TheTask().execute(); 

的AsyncTask

class TheTask extends AsyncTask<Void,Void,Void> 
    { 
     @Override 
     protected void onPreExecute() 
     { 
       super.onPreExecute(); 
       // dispaly progress dialog 
     } 
     @Override 
     protected void doInbackground(Void... params) 
     { 
      // do network related operation here 
      // do not update ui here 
      return null; // return result here 
     } 
     @Override 
     protected void onPostExecute(Void result) // result of background computation received 
     { 
      super.onPostExecute(result); 
      // dimiss dialog 
      // update ui here  
     } 
    } 
+0

好抓!我沒有注意到這個方法沒有被調用。但是當我看到醜陋的'NetworkOnMain'時,我停止了閱讀;} – codeMagic

+0

@Raghunandan請告訴我如何在我的代碼中實現這個。 –

+0

@JohnR使用asynctask我會發佈一個示例。檢查它的理解,然後嘗試 – Raghunandan

2

很難說爲什麼它不工作,因爲你還沒有告訴我們如何 ISN沒有工作。但是,您應該將網絡呼叫轉移到另一個Thread。把它放在AsyncTask。做你的網絡東西doInBackground()

然後,網絡的東西完成後,您可以發送結果到onPostExecute()並從那裏調用startActivity()如果登錄成功。

AsyncTask Docs

AsyncTask example

+0

不錯,你發佈的例子和鏈接我懶惰這次沒有發佈代碼 – Raghunandan

+0

@codeMagic你好嗎? –

+0

@codeMagic我有一點問題希望你能幫助我。我有一個問題,當我輸入密碼顯示數字或字母表然後將其轉換爲點。我想我按任何數字它不顯示數字或字母表只顯示點。 –

2

我的猜測是,你永遠不調用該方法Move_to_next()

我會建議調用它,一旦你從服務器得到了良好的反響,而且還應該採取@ Ragunandan的建議,並在另一個線程上運行請求。

+0

不錯,你指出「你永遠不會調用方法Move_to_next()」。你是對的 – Raghunandan

+0

@GilMoshayof請告訴我如何實現他人建議的代碼。 –

+0

你可以通過合併我的代碼與異步任務來幫助我,我非常感謝你。 –

相關問題