Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 登录不起作用_Java_Android_Login_Http Post_Http Get - Fatal编程技术网

Java 登录不起作用

Java 登录不起作用,java,android,login,http-post,http-get,Java,Android,Login,Http Post,Http Get,我正在尝试制作一个登录应用程序,这样你就可以查看成绩/给老师发电子邮件/等等。 我可以做所有这些,但我不能让登录为我的生活工作。 每次尝试,我都会得到: 无法从会话中检索用户id。用户超时。 大宗报价 我不知道我的代码有什么问题,为什么我不能登录。 有人请帮帮我。多谢各位 主“登录”代码: public class grade可移植性扩展活动{ 私有最终静态字符串站点=”http://dcps.mygradeportal.com/"; 私有文本视图用户名字段、密码字段; @凌驾 创建时的公共vo

我正在尝试制作一个登录应用程序,这样你就可以查看成绩/给老师发电子邮件/等等。 我可以做所有这些,但我不能让登录为我的生活工作。 每次尝试,我都会得到:

无法从会话中检索用户id。用户超时。 大宗报价

我不知道我的代码有什么问题,为什么我不能登录。 有人请帮帮我。多谢各位

主“登录”代码:

public class grade可移植性扩展活动{
私有最终静态字符串站点=”http://dcps.mygradeportal.com/";
私有文本视图用户名字段、密码字段;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySoup.setSite(现场);
usernameField=(TextView)this.findviewbyd(R.id.usernameField);
passwordField=(TextView)this.findViewById(R.id.passwordField);
}
公共无效登录(视图v){
if(usernameField.length()>0&&passwordField.length()>0)
新建Login().execute(新字符串[]{usernameField.getText().toString().trim(),passwordField.getText().toString()});
否则{
Toast.makeText(这是“填写登录表单”,Toast.LENGTH_LONG.show();
}
}
私有类登录扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
锁屏旋转();
dialog=新进度对话框(GradePortalActivity.this);
对话框。setUndeterminate(true);
setMessage(“登录…”);
dialog.show();
}
@凌驾
受保护的布尔doInBackground(字符串…参数){
字符串username=params[0];
字符串密码=参数[1];
试一试{
MySoup.login(用户名、密码);
返回true;
}捕获(例外e){
e、 printStackTrace();
返回false;
}
}
@凌驾
受保护的void onPostExecute(布尔状态){
dialog.dismise();
如果(状态==真){
Toast.makeText(GradePortalActivity.this,“登录”,Toast.LENGTH_LONG.show();
新测试().execute();
}
如果(状态==false){
Toast.makeText(GradePortalActivity.this,“登录失败”,Toast.LENGTH_LONG.show();
}
}
}
私有类测试扩展了异步任务{
私人对话;
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串s=MySoup.inputStreamToString(MySoup.scrape(“http://dcps.mygradeportal.com/homepage.aspx"));
返回s;
}
@凌驾
受保护的void onPostExecute(字符串s){
Toast.makeText(GradePortalActivity.this,s,Toast.LENGTH_LONG).show();
}
}
私有void lockScreenRotation(){
开关(this.getResources().getConfiguration().orientation){
案例配置。方向\u纵向:
此.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u Graphic);
打破
案例配置。方向与景观:
此.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u横向);
打破
}
}
私有void unlockScreenRotation(){
此.setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u未指定);
}   
}
“MYSOUP”(应用程序主干)的代码:

公共类mygroup{
/**http客户端*/
私有静态DefaultHttpClient httpClient=getHttpClient();
/**饼干*/
私有静态列表cookie;
/**http参数*/
私有静态HttpParams HttpParams=httpClient.getParams();
/**用户名*/
私有静态字符串用户名;
/**网站*/
私有静态字符串站点;
/**httpget*/
私有静态HttpGet-HttpGet;
/**回应*/
专用静态HttpResponse响应;
/**实体*/
私有静态HttpEntity;
/**httpost*/
专用静态HttpPost-httpost;
公共静态无效设置位置(字符串s){
如果(!s.endsWith(“/”){
s=s+“/”;
}
如果(!s.startsWith(“http://”)s.startsWith(“https://”){
s=“http://”+s;
}
地点=s;
}
/**
*获取该站点。
* 
*@返回站点
*/
公共静态字符串getSite(){
返回站点;
}
/**
*获取http客户端。
* 
*@返回http客户端
*/
私有静态DefaultHttpClient getHttpClient(){
DefaultHttpClient=新的DefaultHttpClient();
ClientConnectionManager mgr=client.getConnectionManager();
HttpParams params=client.getParams();
client=newdefaulthttpclient(new-ThreadSafeClientConnManager(params,mgr.getSchemeRegistry()),params);
返回客户;
}
/**
*获取会话id。
* 
*@返回会话id
*/
公共静态字符串getSessionId(){
返回cookies.get(0.getValue();
}
/**
*拿到饼干。
* 
*@把饼干还给我
*/
公共静态列表getCookies(){
归还饼干;
}
/**
*检查是否已登录。
* 
*@return true,如果已登录
*/
公共静态布尔值isLoggedIn(){
如果((cookies!=null)&&!cookies.isEmpty())
返回true;
其他的
返回false;
}
/**
*登录。
* 
*@param-url
*网址
*@param用户名
*用户名
*@param密码
*密码
*@throws-CouldNotLoadException
*无法加载异常
*/
公共静态无效登录(字符串用户名、字符串密码)抛出
public class GradePortalActivity extends Activity {
    private final static String SITE = "http://dcps.mygradeportal.com/";
    private TextView usernameField, passwordField;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MySoup.setSite(SITE);
        usernameField = (TextView) this.findViewById(R.id.usernameField);
        passwordField = (TextView) this.findViewById(R.id.passwordField);
    }

    public void login(View v) {
        if (usernameField.length() > 0 && passwordField.length() > 0)
            new Login().execute(new String[] { usernameField.getText().toString().trim(), passwordField.getText().toString() });
        else {
            Toast.makeText(this, "Fill out login form", Toast.LENGTH_LONG).show();
        }
    }

    private class Login extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            lockScreenRotation();
            dialog = new ProgressDialog(GradePortalActivity.this);
            dialog.setIndeterminate(true);
            dialog.setMessage("Logging in...");
            dialog.show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            String username = params[0];
            String password = params[1];
            try {
                MySoup.login(username, password);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean status) {
            dialog.dismiss();
            if (status == true) {
                Toast.makeText(GradePortalActivity.this, "Logged in", Toast.LENGTH_LONG).show();
                new Test().execute();
            }
            if (status == false) {
                Toast.makeText(GradePortalActivity.this, "Log in failed", Toast.LENGTH_LONG).show();
            }
        }
    }

    private class Test extends AsyncTask<String, Void, String> {
        private ProgressDialog dialog;

        @Override
        protected String doInBackground(String... params) {
            String s = MySoup.inputStreamToString(MySoup.scrape("http://dcps.mygradeportal.com/homepage.aspx"));
            return s;
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(GradePortalActivity.this, s, Toast.LENGTH_LONG).show();
        }
    }

    private void lockScreenRotation() {
        switch (this.getResources().getConfiguration().orientation) {
        case Configuration.ORIENTATION_PORTRAIT:
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
        case Configuration.ORIENTATION_LANDSCAPE:
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            break;
        }
    }

    private void unlockScreenRotation() {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }   
}
public class MySoup {

    /** The http client. */
    private static DefaultHttpClient httpClient = getHttpClient();

    /** The cookies. */
    private static List<Cookie> cookies;

    /** The http params. */
    private static HttpParams httpParams = httpClient.getParams();

    /** The username. */
    private static String username;

    /** The SITE. */
    private static String SITE;

    /** The httpget. */
    private static HttpGet httpget;

    /** The response. */
    private static HttpResponse response;

    /** The entity. */
    private static HttpEntity entity;

    /** The httpost. */
    private static HttpPost httpost;

    public static void setSite(String s) {
        if (!s.endsWith("/")) {
            s = s + "/";
        }
        if (!s.startsWith("http://") || s.startsWith("https://")) {
            s = "http://" + s;
        }
        SITE = s;
    }

    /**
     * Gets the site.
     * 
     * @return the site
     */
    public static String getSite() {
        return SITE;

    }

    /**
     * Gets the http client.
     * 
     * @return the http client
     */
    private static DefaultHttpClient getHttpClient() {
        DefaultHttpClient client = new DefaultHttpClient();
        ClientConnectionManager mgr = client.getConnectionManager();
        HttpParams params = client.getParams();



        client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
        return client;
    }

    /**
     * Gets the session id.
     * 
     * @return the session id
     */
    public static String getSessionId() {
        return cookies.get(0).getValue();
    }

    /**
     * Gets the cookies.
     * 
     * @return the cookies
     */
    public static List<Cookie> getCookies() {
        return cookies;
    }

    /**
     * Checks if is logged in.
     * 
     * @return true, if is logged in
     */
    public static boolean isLoggedIn() {
        if ((cookies != null) && !cookies.isEmpty())
            return true;
        else
            return false;
    }

    /**
     * Login.
     * 
     * @param url
     *            the url
     * @param username
     *            the username
     * @param password
     *            the password
     * @throws CouldNotLoadException
     *             the could not load exception
     */
    public static void login(String username, String password) throws Exception {
        String url = SITE;

        try {
            httpget = new HttpGet(url);
            response = httpClient.execute(httpget);
            entity = response.getEntity();

            httpost = new HttpPost(url);
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("userName", username));
            nvps.add(new BasicNameValuePair("password", password));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            response = httpClient.execute(httpost);
            entity = response.getEntity();
            if (entity != null) {
            entity.consumeContent();
            cookies = httpClient.getCookieStore().getCookies();
            }} catch (Exception e) {
            e.printStackTrace();
            throw new Exception("Could not login");
            }

            }



    /**
     * Scrape.
     * 
     * @param url
     *            the url
     * @return the input stream
     */
    public static InputStream scrape(String url) {
        httpget = new HttpGet(url);
        response = null;
        try {
            response = httpClient.execute(httpget);
            entity = response.getEntity();
            InputStream s = entity.getContent();
            System.err.println("encoding " + entity.getContentEncoding());
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * Input stream to string.
     * 
     * @param is
     *            the is
     * @return the string
     */
    public static String inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();

        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return total.toString();
    }

    /**
     * Press link.
     * 
     * @param url
     *            the url
     */
    public static void pressLink(String url) {
        url = SITE + url;
        httpget = new HttpGet(url);
        response = null;
        try {
            response = httpClient.execute(httpget);
            response.getEntity().consumeContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Gets the username.
     * 
     * @return the username
     */
    public static String getUsername() {
        return username;
    }

    /**
     * Sets the session id.
     * 
     * @param sessionId
     *            the new session id
     */
    public static void setSessionId(String sessionId) {
        Cookie cookie = new BasicClientCookie("", sessionId);
        CookieStore cs = new BasicCookieStore();
        cs.addCookie(cookie);
        httpClient.setCookieStore(cs);
        cookies = httpClient.getCookieStore().getCookies();
    }

}
Could not retrieve user_id from the session. User timed out.
String s = MySoup.inputStreamToString(MySoup.scrape("http://dcps.mygradeportal.com/homepage.aspx"));