首页 新闻 会员 周边

安卓app登录账户问题,希望高手多指点

0
悬赏园豆:50 [待解决问题]

我打算给我们学校的成绩查询网站做一个app,结果在登录的时候就遇到了问题.代码是通过疯狂android讲义修改的.登录的时候总是说密码错误,我的程序现在有两个类,Login类是作登录的,HttpUtil类是用来发送请求的,麻烦大神帮看下是什么问题,还有一个问题,就是我提交用户名和密码发送请求的url是web的登录界面还是登录后跳转的界面
package client;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.HttpClient;
import org.json.JSONObject;

import com.example.chengjichaxun.R;

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

public class Login extends Activity {
    //定义输入框
    EditText etNumber,etPass;
    //定义按钮
    Button bnLogin,bnExit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginxml);
        
        etNumber=(EditText)findViewById(R.id.editText_num);
        etPass=(EditText)findViewById(R.id.editText_psw);
        
        bnLogin=(Button)findViewById(R.id.login_button);
        bnExit=(Button)findViewById(R.id.login_exit);
        
        //为按钮绑定监听事件
        //bnExit.setOnClickListener(new HomeListener(this));
        bnLogin.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //校验输入值是否有效
                if (validate()) {
                    //登录成功
                    if(loginpro())
                    {
                        //启动成绩显示页面
                        Intent intent=new Intent(Login.this,ChengJiShow.class);
                        startActivity(intent);
                        //结束当前activity
                        finish();
                    }
                    else {
                        DialogUtil.showDialog(Login.this,"用户名或者密码错误" ,false);
                    }
                }
                
            }
        });
    }

    
    private boolean loginpro()
    {
        //获取用户名和密码
        String username=etNumber.getText().toString();
        String pwd=etPass.getText().toString();
//        Log.v("loginpro", username);
//        Log.v("loginpro", pwd);
        System.out.println("username:"+username);
        System.out.println("password:"+pwd);
        
        JSONObject jsonObject;
        try {
            jsonObject=query(username, pwd);
            //如果userid大于0
            if (jsonObject.getInt("userId")>0) {
                
                return true;
            }
        } catch (Exception e) {
            // TODO: handle exception
            DialogUtil.showDialog(this, "服务器响应异常", false);
            e.printStackTrace();
        }
        return false;
    }
    
    //对用户输入的学号和密码进行校验
    private boolean validate() {
        // TODO Auto-generated method stub
        String username=etNumber.getText().toString().trim();//trim()函数除去字符串开头和末尾的空格或其他字符
        if (username.equals("")) {
            DialogUtil.showDialog(this, "学号不能为空",false);
            return false;
        }
        
        String pwd=etPass.getText().toString().trim();
        if (pwd.equals("")) {
            DialogUtil.showDialog(this, "登录密码不能为空",false);
        }
        
        return true;
    }
    
    //定义发送请求的方法
    private JSONObject query(String username,String password) throws Exception
    {
        //使用Map封装请求参数
        Map<String, String> map =new HashMap<String,String>();
        map.put("username", username);
        map.put("password", password);
        //定义发送请求的url
        String url=HttpUtil.BASE_URL+"getScore.php";
        //String url=HttpUtil.BASE_URL;
        //发送请求
        return new JSONObject(HttpUtil.postRequest(url,map));
    }

}



package client;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

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.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.R.string;
import android.util.Log;

public class HttpUtil {
    // 创建HttpClient对象
    public static HttpClient httpClient = new DefaultHttpClient();
    public static final String BASE_URL = "http://online.gxut.edu.cn/chengji/";
    //public static final String BASE_URL ="https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn/";

    
    /**
     * get形式发送请求
     * @param url
     * @return
     * @throws Exception
     */

    public static String getRequest(final String url) throws Exception {
        FutureTask<String> task = new FutureTask<String>(
                new Callable<String>() {
                    @Override
                    public String call() throws Exception {
                        // 创建HttpGet对象
                        HttpGet get = new HttpGet(url);
                        // 发送get请求
                        HttpResponse httpResponse = httpClient.execute(get);
                        // 如果服务器成功的返回响应
                        if (httpResponse.getStatusLine().getStatusCode() == 200) {
                            // 获取服务器响应字符串
                            String result = EntityUtils.toString(httpResponse
                                    .getEntity());
                            return result;
                        }
                        return null;
                    }
                });
        new Thread(task).start();
        return task.get();
    }
    
    /**
     * post形式发送请求
     */
    public static String postRequest(final String url
            , final Map<String ,String> rawParams)throws Exception
        {
            FutureTask<String> task = new FutureTask<String>(
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    // 创建HttpPost对象。
                    HttpPost post = new HttpPost(url);
                    // 如果传递参数个数比较多的话可以对传递的参数进行封装
                    List<NameValuePair> params =
                        new ArrayList<NameValuePair>();
                    for(String key : rawParams.keySet())
                    {
                        //封装请求参数
                        params.add(new BasicNameValuePair(key
                            , rawParams.get(key)));
                    }
                    // 设置请求参数
                    post.setEntity(new UrlEncodedFormEntity(
                        params, "utf-8"));
                    // 发送POST请求
                    HttpResponse httpResponse = httpClient.execute(post);
                    // 如果服务器成功地返回响应
                    if (httpResponse.getStatusLine()
                        .getStatusCode() == 200)
                    {
                        // 获取服务器响应字符串
                    
                        String result = EntityUtils
                            .toString(httpResponse.getEntity());
                        return result;
                    }
                    return null;
                }
            });
            new Thread(task).start();
            return task.get();
        }

}
下面这个图是我在web端登录账号密码之后的信息

qq199208的主页 qq199208 | 初学一级 | 园豆:152
提问于:2015-03-22 10:11
< >
分享
所有回答(3)
0

状态码为200,证明你请求成功了,提示密码错误可能性为你的加密方式和后台不对应,或者是你登陆密码原本就是错误的。

jaden.xu | 园豆:202 (菜鸟二级) | 2015-03-22 15:29
0

对的  如果说这个请求返回的状态码是200 说明请求没问题,后台能正常通信的。

宫保鸡丁 | 园豆:202 (菜鸟二级) | 2015-03-22 22:15
0

你是基于现有的web系统后台实现app功能对吗?

1  app提交用户名和密码发送请求的url一般对应学校网站登录时的url,你可以打开网站登录页面,看下点击登录按钮时发生请求的url。

2  app若登录成功,后续的请求操作都需要带上会话信息,一般的网站后台都通过session的方式实现,这时你需要研究下登录时返回的信息了,看看cookie值

3  如果还不行,找找负责学校网站后台的技术,可能有更好的思路~~

美码师 | 园豆:475 (菜鸟二级) | 2015-03-24 00:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册