当前位置:编程学习 > wap >>

初学andriod的小问题,求指教。

最近没事,想用做个andriod应用,登陆学校教务系统,然后查找课程成绩,可刚刚在网上找代码时,自己改了下,可是没反应,我会点java,求解释,代码有问题吗?
package cn.yzz.AndroidLogin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class AndroidLoginActivity extends Activity {
//属性
private EditText username;
private EditText password;
private Button login;
private Intent intent;
List<Cookie> cookies;                      //保存获取的cookie
HttpClient client = new DefaultHttpClient();                 
HttpResponse httpResponse;   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //添加登录按钮监听
        login = (Button)findViewById(R.id.userlogin);
        login.setOnClickListener(ocl);
    }
    //创建按钮监听器对象
    OnClickListener ocl = new OnClickListener(){
public void onClick(View arg0) {
String uriAPI = "http://bkjw.xznu.edu.cn/Login.aspx";
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
/* 建立HTTP Post连线 */
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
/**
 * 以下三个数据就是我们的之前在POST里的数据,不用在意验证码
 */
params.add(new BasicNameValuePair("Account",username.getText().toString()));
params.add(new BasicNameValuePair("PWD",password.getText().toString()));
try {
// 发出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 取得HTTP response
httpResponse = client.execute(httpRequest);   //执行
// 若状态码为302 ok
if (httpResponse.getStatusLine().getStatusCode() == 302) {   //返回值正常

System.out.println("登录成功");
intent = new Intent(AndroidLoginActivity.this, SencondActivity.class);
//启动Activity
startActivity(intent);

}
else {
Toast.makeText(AndroidLoginActivity.this,"用户登录信息错误" , Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}

}
    };
    
}
--------------------编程问答--------------------  HttpPost httpRequest = new HttpPost(uriAPI);  你貌似把这句注释了 --------------------编程问答--------------------
引用 1 楼 wangmiaowangjianqian 的回复:
 HttpPost httpRequest = new HttpPost(uriAPI);  你貌似把这句注释了


在eclipse里没显示注掉 --------------------编程问答-------------------- xml文件改了吗 --------------------编程问答-------------------- 大牛们快来解决吧,我也来学习学习 --------------------编程问答--------------------
引用 3 楼 sj1094838711 的回复:
xml文件改了吗

改了,网上说是andriod4.o以后要开线程 --------------------编程问答-------------------- new个线程放在handler里让运行试试,注意权限。 --------------------编程问答--------------------
引用 5 楼 lixiangwei1989 的回复:
Quote: 引用 3 楼 sj1094838711 的回复:

xml文件改了吗

改了,网上说是andriod4.o以后要开线程


是的 修改一下代码


//创建按钮监听器对象
    OnClickListener ocl = new OnClickListener(){
public void onClick(View arg0) {
                        Thread thread = new Thread(){
                            public void run(){
String uriAPI = "http://bkjw.xznu.edu.cn/Login.aspx";
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
/* 建立HTTP Post连线 */
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
/**
 * 以下三个数据就是我们的之前在POST里的数据,不用在意验证码
 */
params.add(new BasicNameValuePair("Account",username.getText().toString()));
params.add(new BasicNameValuePair("PWD",password.getText().toString()));
try {
// 发出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 取得HTTP response
httpResponse = client.execute(httpRequest);   //执行
// 若状态码为302 ok
if (httpResponse.getStatusLine().getStatusCode() == 302) {   //返回值正常

System.out.println("登录成功");
intent = new Intent(AndroidLoginActivity.this, SencondActivity.class);
//启动Activity
startActivity(intent);

}
else {
Toast.makeText(AndroidLoginActivity.this,"用户登录信息错误" , Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}

}
}
};
thread.start();
    };
    
--------------------编程问答-------------------- 在Android中,是不允许在UI线程中直接更新界面组件的,向楼上的new一个线程,在线程中,进行网络编程等耗时任务,再发送一个Message,交给Handler类的handleMessage方法处理UI组件,这样分工更明确,耦合度较低 --------------------编程问答-------------------- if (httpResponse.getStatusLine().getStatusCode() == 302) {   //返回值正常 
我怎么记得返回值是200才表示正常 --------------------编程问答--------------------
引用 9 楼 YJun2012 的回复:
if (httpResponse.getStatusLine().getStatusCode() == 302) {   //返回值正常 
我怎么记得返回值是200才表示正常

我们学校我用抓包工具看到返回的是302 --------------------编程问答-------------------- 是不是学校网站有验证码,所以不行啊
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,