我正在尝试使用json和GET请求在Android应用程序中执行简单的登录过程 我从主活动中获取用户名和密码然后将其传递给登录类,该登录类是Async的子类,用于登录操作etablich 之后我开始创建一个名为Profile的新Activity,我将显示一些数据,但它不能用于我的代码:
package com.example.socialnetcit;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;
public class Login extends Activity {
Tools tools = new Tools();
EditText edtname;
EditText edtpass;
String name;
String pass;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button loginBtn = (Button) findViewById(R.id.button1);
loginBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edtname = (EditText) findViewById(R.id.editText1);
edtpass = (EditText) findViewById(R.id.editText2);
name = edtname.getText().toString();
pass = edtpass.getText().toString();
if (tools.isEmpty(edtname) || tools.isEmpty(edtpass))
{
Toast.makeText(getApplicationContext(),
"Plz enter user name and password",
Toast.LENGTH_LONG).show();
} else {
String url = "http://example.com/Andoid/login.php?"
+ "name=" + name + "&pass=" + pass;
login log = new login();
log.execute(url);
}
}
});
}
////// Login class
class login extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
HttpEntity httpEntity = null;
JSONObject jsonObj = null;
Boolean login = false;
try {
DefaultHttpClient httpClient = new DefaultHttpClient(); // HttpClient
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
jsonObj = new JSONObject(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String tmp = tools.JsonVerify(jsonObj).toString();
if (tmp.equals("true")) {
Log.d("Testy", "login successful");
login = true;
}
}
return login;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result == true) {
Intent i=new Intent(Login.this, Profile.class);
startActivity(i);
finish();
} else {
Toast.makeText(getApplicationContext(),
"Wrong password or username ", Toast.LENGTH_LONG)
.show();
}
}
}
}
关闭当前活动(登录)并返回第一个(登录/注册后选择的主电源)
这是日志数据:
12-17 01:24:11.080: I/ActivityManager(1723): START u0 {cmp=com.example.socialnetcit/.Profile} from pid 6126
12-17 01:24:11.080: D/ActivityManager(1723): TopActivityInfo, pkgName: com.example.socialnetcit activityName: com.example.socialnetcit.Profile bstSpecialAppKeyboardHandlingEnabled = false
12-17 01:24:11.080: D/ActivityManager(1723): Showing guidance for pkgName: com.example.socialnetcit
12-17 01:24:11.100: D/GuidanceScreen(2000): event === app_launch
12-17 01:24:11.100: D/GuidanceScreen(2000): hiding guidance
12-17 01:24:11.100: D/GuidanceScreen(2000): hardKeyboard = 1
12-17 01:24:11.100: D/GuidanceScreen(2000): controllerType === DualAction
12-17 01:24:11.100: D/GuidanceScreen(2000): appName: SocialNetCIT, currentPkg: com.example.socialnetcit, event: app_launch, controller: DualAction
12-17 01:24:11.100: D/GuidanceScreen(2000): appName: SocialNetCIT
12-17 01:24:11.110: D/GuidanceScreen(2000): no guidance for com.example.socialnetcit.
12-17 01:24:11.620: W/ActivityManager(1723): Activity pause timeout for ActivityRecord{4b0f4214 u0 com.example.socialnetcit/.Login t29}
12-17 01:24:30.820: D/dalvikvm(1970): GC_FOR_ALLOC freed 761K, 30% free 2770K/3936K, paused 10ms, total 10ms
12-17 01:25:26.400: D/dalvikvm(2410): GC_FOR_ALLOC freed 777K, 29% free 2809K/3936K, paused 0ms, total 0ms
12-17 01:25:30.930: D/dalvikvm(1970): GC_FOR_ALLOC freed 765K, 31% free 2753K/3936K, paused 0ms, total 0ms
执行后:
Intent i=new Intent(Login.this, Profile.class);
startActivity(i);
finish();
答案 0 :(得分:1)
它会关闭Login活动,因为您调用了finish()
:
if (result == true) {
Intent i=new Intent(Login.this, Profile.class);
startActivity(i);
finish();
}
根据您的评论,听起来您希望看到个人资料活动,但您会看到主要活动。您应该查看个人资料活动,看看它为什么要完成。