Java 从asynctask类调用onPostExecute方法中的活动
我在使用标题中的方法时遇到问题。 在我的android应用程序项目中,当我收到一条来自.php脚本的成功消息时,我想用这个方法启动一个新的活动。消息是从php脚本正确接收到的,但无论我尝试了什么,我都无法在方法onPostExecute上启动新活动。 这是我的代码的外观,请有经验的人看一下,并告诉我出了什么问题。 提前感谢您抽出时间Java 从asynctask类调用onPostExecute方法中的活动,java,php,android,Java,Php,Android,我在使用标题中的方法时遇到问题。 在我的android应用程序项目中,当我收到一条来自.php脚本的成功消息时,我想用这个方法启动一个新的活动。消息是从php脚本正确接收到的,但无论我尝试了什么,我都无法在方法onPostExecute上启动新活动。 这是我的代码的外观,请有经验的人看一下,并告诉我出了什么问题。 提前感谢您抽出时间 public class BackGround extends AsyncTask<String, Void, String> { Cont
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();
}
}
公共类后台扩展异步任务{
上下文ctx;
背景(上下文)
{
这个.ctx=上下文;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串名称=参数[0];
字符串密码=参数[1];
字符串数据=”;
int tmp;
试一试{
URL=新URL(“http://webserver.xy/login_wellness.php");
字符串urlParams=“email=“+name+”&password=“+password;
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os=httpURLConnection.getOutputStream();
write(urlParams.getBytes());
os.flush();
os.close();
InputStream=httpURLConnection.getInputStream();
而((tmp=is.read())!=-1){
数据+=(字符)tmp;
}
is.close();
httpURLConnection.disconnect();
返回数据;
}捕获(格式错误){
e、 printStackTrace();
return“Exception:+e.getMessage();
}捕获(IOE异常){
e、 printStackTrace();
返回“Exceptionn:+e.getMessage();
}
}
@凌驾
受保护的void onPreExecute(){
}
@凌驾
受保护的void onPostExecute(字符串结果){
如果(结果等于(“成功”))
{
Toast.makeText(ctx,“onpost执行”,Toast.LENGTH_LONG.show();
意向意向=新意向(ctx,MainActivity.class);
//intent.addFlags(intent.FLAG\u活动\u新任务);
星触觉(意图);
}
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
将构造函数更改为
BackGround(Context context)
{
this.ctx=context.getApplicationContext();
}
然后
Intent intent = new Intent(ctx,MainActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
希望这对您有所帮助。如果您使用的是单独的类,只需通过构造函数传递该类即可 比如说, 您的异步类: 您的构造函数:
BackGround(Context context,Class mIntentclass) {
this.ctx=context;
this.mIntentclass = mIntentclass;
}
@凌驾
受保护的void onPostExecute(字符串结果){
您的需求类:
你想打哪儿就打哪儿
像
希望它能帮助你!检查toast的结果。它正确地显示为“success”?后台类处于活动或单独的JAVA中class@Vijaykumar是的,我收到了成功消息。@AbdulKawee后台类位于单独JAVA类中的活动之外。如果使用单独的类,则必须通过参数传递意图。我以前已经这样做过,但它不起作用。toast来自if块内部
if(result.equals(“success”){Toast.makeText(ctx,“onpost excute”,Toast.LENGTH_LONG).show();Intent Intent Intent=newintent(ctx,MainActivity.class);//Intent.addFlags(Intent.FLAG_ACTIVITY_new_TASK);ctx.startActivity(Intent);}
它没有被执行它应该已经工作了,您从哪里调用这个类,或者ctx是否为null?您是否尝试过用getApplicationContext()更改构造函数??ctx值不为null,并且在构造函数中我尝试过包含getApplicationContext()而且它也不起作用,这是同样的问题-它不是进入if块,而是if块外的toast,它可以很好地显示成功消息。问题是字符串结果的比较它没有与单词“success”进行比较。代码直接跳转到else而不是测试if条件。if(result.equals(“success”)){Toast.makeText(ctx,“onpost excute”,Toast.LENGTH_LONG).show();Intent Intent Intent=new Intent(ctx,MainActivity.class);ctx.startActivity(Intent);}else{Toast.makeText(ctx,“Valoare lui result in else”+result,Toast.LENGTH_LONG.show()
存储服务器响应字符串数据的位置=“”这个对不对?这就是问题所在,现在它在全球范围内运行良好!非常感谢你的帮助,伙计,为此干杯!:)
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);