登录活动成功后如何启动活动。。我试过这个,如何在登录后开始一个活动活动,但似乎对我不起作用。我已经把登录活动放在了我的主页中。。然后我在php脚本中的警报消息。。这是我的backgroundtask
编码:
@Override
protected String doInBackground(String... params) {
String reg_url = "http://10.0.2.2/webapp/register.php";
String login_url = "http://10.0.2.2/webapp/login.php";
String method = params[0];
if(method.equals("register"))
{
String name = params[1];
String user_name = params[2];
String user_pass = params [3];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8") + "=" + URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("user_name","UTF-8") + "=" + URLEncoder.encode(user_name,"UTF-8")+"&"+
URLEncoder.encode("user_pass","UTF-8") + "=" + URLEncoder.encode(user_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if (method.equals("login"))
{
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection =(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response = "";
String line = "";
while ((line = bufferedReader.readLine())!=null)
{
response+= line ;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Success..."))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else if(result.contains("Login Success"))
{
Intent i = new Intent(getApplicationContext,NewActivity.class);
getApplicationContext.startActivity(i);
alertDialog.setMessage(result);
alertDialog.show();
}
}
}
请帮我做这个。。。一个星期以来,我一直在努力解决这个问题。。tq
在登录过程中,当登录成功时,您尝试调试"响应"(第66行)的值
这是调试"响应"的两种方法
- 使用断点调试工具
-
在返回响应之前,请将下面的代码放在下面并观察您的Logcat。
Log.e("代码"、"响应:"+响应);
当您知道响应的内容时,将onPostExecute更改为以下代码,并将{$CONTENTS you DEBUGED}替换为调试结果。
@Override
protected void onPostExecute(String result) {
if (result.equals("Registration Success...")) {
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} else if(result.contains("Hello") {
Intent i = new Intent(ctx, SomethingClass.class);
ctx.startActivity(i);
} else {
alertDialog.setMessage(result);
alertDialog.show();
}
}
编辑))
响应模式为Hello Welcome+$user。因此,当响应包含"Hello"时,它将移动另一个活动。就是这样。
必须在void doInBackGround上返回String"Login success",然后在void onPostExecute上创建新条件;
@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Success..."))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
} else if (result.equals("Login success"))
{
startActivity(new Intent(yourCURRENTactivity.this, yourNEXTactivity.class));
}
else
{
alertDialog.setMessage(result);
alertDialog.show();
}
}
}
在您的代码中,您故意给定了应用程序上下文,这是不对的。它应该是当前活动,然后是下一个切换活动。请参阅下面的代码
@Override
protected void onPostExecute(String result) {
if(result.equals("Registration Success..."))
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
else if(result.contains("Login Success"))
{
Intent i = new Intent(currentactivity.this,NewActivity.class);
getApplicationContext.startActivity(i);
alertDialog.setMessage(result);
alertDialog.show();
}
}