Android HTTP请求始终返回代码404

时间:2012-06-06 17:18:26

标签: android

我正在尝试实现一个简单的HTTP请求。该请求将验证来自服务器的用户名和密码。服务器配置为只返回两个错误代码,200个用于验证,404个用于其他所有错误代码。我已经用eclipse进行了大量的调试,发现我总是收到代码404.在发布之前我已经阅读了所有其他现有的问题。

private class loginTask extends AsyncTask<String, Integer, Boolean> {
    String check;
    @Override
    protected Boolean doInBackground(String... arg0) {
                ("http://www.unite.upmc.edu/account/IPhoneLogin?username=username&password=password

        String username = arg0[0];
        String password = arg0[1];
        URI uri = null;
        try {
            uri = new URI("http://unite.upmc.edu/account/IPhoneLogin?"+"username="+username+"&"+"password="+password);// I know the method is called IPhoneLogin, it's being used by both Android and IOS clients
        } catch (URISyntaxException e1) {

            e1.printStackTrace();
        }
        HttpClient client = new DefaultHttpClient();

        try {//Look here

            HttpResponse response = client.execute(new HttpGet(uri));
            String code = "" + response.getStatusLine().getStatusCode();
            check = code;
            if(code.equals("200")){
                loggedIn = true;
            }else{
                                    //need to re-enter username and password
            }
        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }


        hasPin = false;
        return true;
    }

    @Override
    protected void onProgressUpdate(Integer... integers) {
        setProgress(integers[0]);
    }

    @Override
    protected void onPreExecute() {

        ProgressDialog dialog = new ProgressDialog(UniteActivity.this);
        dialog.setMessage("Logging in...." + check);
        dialog.show();

    }

我知道服务器已启动且密码和用户名是正确的,因为我使用Internet Explorer正常登录了很多次。我正在使用带有Android SDK的eclipse IDE,并担心这可能是它提供的bug模拟器的产品。关于我做错了什么想法?

1 个答案:

答案 0 :(得分:0)

在doInBackground方法中尝试此代码以获取响应代码

url = new URL(mUrl);

            Log.i(LOG_TAG, url+"");
            HttpGet method= new HttpGet(new URI(mUrl));
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(mUrl));
            HttpResponse response = client.execute(method);
            responseCode = response.getStatusLine().getStatusCode();

希望这样可以正常工作。