小白在做一个模拟登录教务系统的app,可是在post上遇到了问题,理想是如果登录成功了就跳转到logi_success的页面,然后将获得的html源码直接贴上(之后json解析),如果失败的话就跳转到login_fault页面,点击“返回”按钮返回到登录页面
然后我现在遇到的问题是:不知道是数据传递的问题还是其他?我点击登录没有反应,logcat也米有错误提示,不知道是不是获取不了数据还是怎样,希望各位大神指点下小白,谢谢
下面贴代码:
MainActivity.java:
public class MainActivity extends Activity { public static final int SHOW_RESPONSE=0; private EditText user_id; private EditText password; private EditText validate; private ImageView photo; private CheckBox keep; private Button next_botton; private Button login_botton; private Bitmap showVerifation; private boolean flag; // http://jwxt.jmpt.cn:8125/JspHelloWorld/authImg 另一个验证码地址 // http://jwxt.jmpt.cn:8125/JspHelloWorld/authImg?now=Wed%20Jun%2001%202016%2022:42:07%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4 // 另一个验证码地址 String VERIFATIONURL = "http://jwxt.jmpt.cn:8125/JspHelloWorld/authImg?now=Wed%20Jun%2001%202016%2022:42:07%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4";// 验证码地址 String LOGINURL = "http://jwxt.jmpt.cn:8125/JspHelloWorld/login.jsp";// 登录页面 String MAINBODYHTML = "";// 获得个人主界面的HTML @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); user_id = (EditText) findViewById(R.id.user_id); password = (EditText) findViewById(R.id.password); validate = (EditText) findViewById(R.id.validate); login_botton = (Button) findViewById(R.id.login_botton); next_botton = (Button) findViewById(R.id.next_botton); keep = (CheckBox) findViewById(R.id.keep); photo = (ImageView) findViewById(R.id.photo); login_botton.setOnClickListener(onClickListener);// 将EditText里的信息提交按钮 next_botton.setOnClickListener(onClickListener);// 刷选验证码按钮 //initView(); } View.OnClickListener onClickListener = new View.OnClickListener() { private boolean flase; @Override public void onClick(View v) { switch (v.getId()) { case R.id.login_botton: if (equals()) { String user_id1 = (String) user_id.getEditableText() .toString(); String password1 = (String) password.getEditableText() .toString(); String validate1 = (String) validate.getEditableText() .toString(); DoLogin(user_id1, password1, validate1); // Intent intent = new Intent(MainActivity.this,login_fault.class); // startActivity(intent); } break; case R.id.next_botton: DoGetVerifation(); break; } } private boolean equals() { // TODO Auto-generated method stub if (user_id.getEditableText().toString().equals("")) { Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT) .show(); flag = flase; } else if (password.getEditableText().toString().equals("")) { Toast.makeText(MainActivity.this, "密码不能为空", Toast.LENGTH_SHORT) .show(); flag = flase; } else if (validate.getEditableText().toString().equals("")) { Toast.makeText(MainActivity.this, "验证码不能为空", Toast.LENGTH_SHORT) .show(); flag = flase; } else { flag = true; } return flag; } private void DoLogin(final String user_id1, final String password1, final String validate1) { // TODO Auto-generated method stub new Thread(new Runnable() { @Override public void run() { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse; HttpPost httpRequest = new HttpPost(LOGINURL); // 设置post参数 List params = new ArrayList(); params.add(new BasicNameValuePair("actionId", "login")); params.add(new BasicNameValuePair("actionmi", "m10")); params.add(new BasicNameValuePair("osname", "Win7,?豸:pc")); params.add(new BasicNameValuePair("pageId", "000101")); params.add(new BasicNameValuePair("Yhm", "ѧ ?ţ?")); params.add(new BasicNameValuePair("radiobutton", "student")); params.add(new BasicNameValuePair("password", password1)); params.add(new BasicNameValuePair("validate", validate1)); params.add(new BasicNameValuePair("username", user_id1)); httpRequest.setHeader("Cookie", "JSESSIONID=44983DD250A48E966B95381E2C472C29"); httpRequest.setHeader("Host", "jwxt.jmpt.cn:8125"); httpRequest.setHeader("Referer", "http://jwxt.jmpt.cn:8125/JspHelloWorld/login.jsp"); httpRequest .setHeader( "User-Agent", "Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; BOIE9;ZHCN; rv:11.0) like Gecko"); try { // 发出HTTP request httpRequest.setEntity(new UrlEncodedFormEntity(params, "GBK")); // 取得HTTP response httpResponse = httpClient.execute(httpRequest); // 执行 // 若状态码为200 ok if (httpResponse.getStatusLine().getStatusCode() == 200) { // 返回值正常 HttpEntity entity=httpResponse.getEntity(); String response=EntityUtils.toString(entity,"GBK"); Message message=new Message(); message.what=SHOW_RESPONSE; message.obj=response.toString(); Handler handler=new Handler(){ public void handleMessage (Message msg){ switch(msg.what){ case SHOW_RESPONSE: String response=(String)msg.obj; Intent intent = new Intent(MainActivity.this,login_success.class); startActivity(intent); } } }; handler.sendMessage(message); } else { //Toast.makeText(MainActivity.this,"dsadsa",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this,login_fault.class); startActivity(intent); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } private void DoGetVerifation() { // TODO Auto-generated method stub final Handler handler = new Handler() { @SuppressWarnings("deprecation") public void handleMessage(android.os.Message msg) { if (msg.arg1 == 1) { Bitmap bitmap = (Bitmap) msg.obj; Drawable pic = new BitmapDrawable(bitmap); // loginpic.setImageBitmap(bitmap); photo.setBackgroundDrawable(pic); } }; }; final String urlString = "http://jwxt.jmpt.cn:8125/JspHelloWorld/authImg?now=Wed%20Jun%2001%202016%2022:42:07%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4"; new Thread() { public void run() { Message message = handler.obtainMessage(); message.obj = HttpBitmap.getHttpBitmap(urlString); message.arg1 = 1; handler.sendMessage(message); }; }.start(); } }; }
点击登录按钮的话logcat只有这些信息
对了,cookie的值不变,我也不知道为什么,反正用httpwatch查看的都是这个值
Add Looper.prepare() before Handler handler=new Handler(){...};; add Looper.loop() after the end;
or directly Handler handler = new Handler(Looper.getMainLooper()){ ...};
I don’t know how you capture the web page data. Generally, you don’t need so many posts. Also, the cookie you set seems to have no meaning.
Of course, the most important thing is the analysis of the website login process itself. If this is accurate, most of it will be done.
Httpclient is not very easy to use. Go to okhttp, make a tool class, write all these operations, and use the interface to complete the callback, otherwise you will not understand the activity in the end...