我在使用標題中的方法時遇到問題。 在我的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();
}
}
檢查吐司結果是否正確顯示「成功」? –
BackGround Class在活動或單獨的JAVA類 –
@Vijaykumar是的,我收到成功的消息。 – skynet