问题描述
- android开发,模拟登录时的问题,求助大神~~
-
尝试做一个学校的教务系统android app,模拟登陆过程都没有问题,使用的是asynhttpclient。只是登陆成功后,只会跳转至如图中(2)这个url中,里面content的内容只是是:{success:true,tips:'登录成功',newsms:0,uid:'xxxxxxxxx'},没有其他内容。所以AsyncHttpResponseHandler的onSuccess()方法得到的也只是这个页面的信息,而我想要进一步操作查询成绩或课表则需要跳转到后面(3)这几个URL,不知如何从代码中实现。我想过直接使用(3)中的url,然后传入get(urlString,params,AsyncHttpResponseHandler)方法直接操作,但是如图所示,(3)中每个url后面都有个_dc="xxxxxxxxxxx"这个参数,也就是Query String中的信息,而这个每次都是随机的,所以我也没法取得参数带入到params中,现在举步维艰.....所以求助大神我到这一步应该如何解决
下面是我的代码:public class LoginActivity extends Activity { private EditText username, password; private Button login; private PersistentCookieStore cookie; private SQLiteDatabase db; private LinkService linkService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); Log.i("tag", "create"); initValue();// 变量初始化 initView();// 视图初始化 initCookie(this);// cookie初始化 initEvent();// 事件初始化 initDatabase();// 数据库初始化 } private void initValue() { linkService = LinkService.getLinkService(); } private void initView() { username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); login = (Button) findViewById(R.id.login); } private void initCookie(Context context) { Log.i("tag", "执行了initCookie"); cookie = new PersistentCookieStore(context); HttpUtil.getClient().setCookieStore(cookie); getCookieText(); } /** * 获取标准 Cookie */ private String getCookieText() { List<Cookie> cookies = cookie.getCookies(); Log.d("getCookieText", "cookies.size() = " + cookies.size()); Utils.setCookies(cookies); StringBuffer sb = new StringBuffer(); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); String cookieName = cookie.getName(); String cookieValue = cookie.getValue(); if (!TextUtils.isEmpty(cookieName) && !TextUtils.isEmpty(cookieValue)) { sb.append(cookieName + "="); sb.append(cookieValue + ";"); } } Log.e("getCookieText", sb.toString()); return sb.toString(); } private void initEvent() { login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { login(); } }); } private void initDatabase() { db = Connector.getDatabase(); } /** * 跳转至主页 */ private void jump2Main() { SharedPreferenceUtil util = new SharedPreferenceUtil(getApplicationContext(), "accountInfo"); util.setKeyData("username", HttpUtil.j_username); util.setKeyData("password", HttpUtil.j_password); util.setKeyData("isLogin", "TRUE"); Intent intent = new Intent(LoginActivity.this, EduMainActivity.class); startActivity(intent); finish(); } private void login() { HttpUtil.j_username = username.getText().toString().trim() + ",undergraduate"; HttpUtil.j_password = password.getText().toString().trim(); if (TextUtils.isEmpty(HttpUtil.j_username) || TextUtils.isEmpty(HttpUtil.j_password)) { Toast.makeText(getApplicationContext(), "账号或密码不可以为空!", Toast.LENGTH_SHORT).show(); return; } final ProgressDialog dialog = CommonUtil.getProcessDialog(LoginActivity.this, "正在登录中!"); dialog.show(); RequestParams params = HttpUtil.getLoginRequestParams();// 获得请求参数 HttpUtil.getClient().setURLEncodingEnabled(true); HttpUtil.post(HttpUtil.URL_LOGIN, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int arg0, Header[] arg1, byte[] arg2) { try { String resultContent = new String(arg2, "UTF-8"); Log.i("tag", resultContent); if (resultContent.contains("tips:'登录成功'")) { Toast.makeText(getApplicationContext(), "登录成功!!!", Toast.LENGTH_SHORT).show(); getCookieText(); jump2Main(); } else { Toast.makeText(getApplicationContext(), "账号或者密码错误!!!", Toast.LENGTH_SHORT).show(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } finally { dialog.dismiss(); } } @Override public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) { Toast.makeText(getApplicationContext(), "登录失败!!!!", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); } @Override public void onBackPressed() { Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right); finish(); } }
解决方案
解决方案二:
Robolectric 这个自动化测试框架可以解决你的问题,带有离线http的返回设置
时间: 2024-10-27 15:58:30