2017-04-22 81 views
0

我在使用標題中的方法時遇到問題。 在我的android應用程序項目中,當我從.php腳本收到成功的消息時,我想使用此方法啓動新的活動。該消息是從PHP腳本正確接收,但我無法啓動方法onPostExecute上的新活動,無論我嘗試過。 這是它看起來我的代碼,有人更有經驗的看看它,並告訴我什麼是錯的。 預先感謝您的時間。來自asynctask類的onPostExecute方法中的呼叫活動

public class BackGround extends AsyncTask<String, Void, String> { 


    Context ctx; 

    BackGround(Context context) 
    { 
     this.ctx=context; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String name = params[0]; 
     String password = params[1]; 

     String data = ""; 
     int tmp; 

     try { 
      URL url = new URL("http://webserver.xy/login_wellness.php"); 
      String urlParams = "email=" + name + "&password=" + password; 

      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
      httpURLConnection.setDoOutput(true); 

      OutputStream os = httpURLConnection.getOutputStream(); 
      os.write(urlParams.getBytes()); 
      os.flush(); 
      os.close(); 
      InputStream is = httpURLConnection.getInputStream(); 
      while ((tmp = is.read()) != -1) { 
       data += (char) tmp; 
      } 
      is.close(); 
      httpURLConnection.disconnect(); 

      return data; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      return "Exception: " + e.getMessage(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return "Exceptionn: " + e.getMessage(); 
     } 

    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onPostExecute(String result) {  
    if(result.equals("success")) 

    { 
     Toast.makeText(ctx,"onpost excute",Toast.LENGTH_LONG).show();   
     Intent intent = new Intent(ctx,MainActivity.class); 
     // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     ctx.startActivity(intent); 
    } 

    Toast.makeText(ctx,result,Toast.LENGTH_LONG).show(); 
    } 

} 
+0

檢查吐司結果是否正確顯示「成功」? –

+0

BackGround Class在活動或單獨的JAVA類 –

+0

@Vijaykumar是的,我收到成功的消息。 – skynet

回答

0

如果您使用單獨的類,則只需通過構造函數傳遞該類。

例如,

你異步類:

你的構造:

BackGround(Context context,Class mIntentclass) { 
    this.ctx=context; 
    this.mIntentclass = mIntentclass; 

} 

@覆蓋 保護無效onPostExecute(字符串結果){

if(result.equals("success")) { 
    Toast.makeText(ctx,"onpost excute",Toast.LENGTH_LONG).show();   
    Intent intent = new Intent(ctx,mIntentclass.class); 
    ctx.startActivity(intent); 

} 
    Toast.makeText(ctx,result,Toast.LENGTH_LONG).show(); 
} 

你需要的類別:

你只是打電話到你想要的地方嗎?

BackGround background = new BackGround(context,MainActivity); 

希望其幫助ü!

+0

問題是比較字符串結果,它不會與單詞「成功」進行比較。 代碼直接跳轉到其他條件不測試。 'if(result.equals(「success」)) { Toast.makeText(ctx,「onpost excute」,Toast.LENGTH_LONG).show(); Intent intent = new Intent(ctx,MainActivity.class); ctx.startActivity(intent); } else { Toast.makeText(ctx,「Valoare lui result else」+ result,Toast.LENGTH_LONG).show(); }' – skynet

+0

你在哪裏存儲服務器響應字符串數據=「」這一個正確與否? –

+0

這是正確的內部String data =「」 – skynet

0

更改您的構造函數

BackGround(Context context) 
{ 
    this.ctx=context.getApplicationContext(); 
} 

然後

Intent intent = new Intent(ctx,MainActivity.class); 
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
ctx.startActivity(intent); 

希望這有助於。

+0

我以前做過,但它不工作。 的吐司從內側的if塊 '如果(result.equals( 「成功」)) { Toast.makeText(CTX 「onPOST等EXCUTE」,Toast.LENGTH_LONG).show(); Intent intent = new Intent(ctx,MainActivity.class); // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(intent); }' 它沒有執行 – skynet

+0

它應該起作用,你從哪裏調用這個類,或者是否爲ctx null? –

+0

你有沒有嘗試更改構造函數?使用getApplicationContext() –