Android登录未授权

时间:2013-05-14 03:54:34

标签: android http authentication login

我在连接到Web服务的android项目上工作。用户登录后有一个显示数据的功能。登录功能使用http post方法调用Web服务。这是我连接到服务器的登录代码:

public HttpResponse makeRequestNew(String path, String params, String params2) throws ClientProtocolException, IOException 
    {

    httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(path);
    httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
    List<NameValuePair> entity = new ArrayList<NameValuePair>();
    entity.add(new BasicNameValuePair("name", params));
    entity.add(new BasicNameValuePair("pass", params2));


    try {
        httppost.setEntity(new UrlEncodedFormEntity(entity));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Execute HTTP Post Request
    /*
    try {
        return httpclient.execute(httppost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    */
    return httpclient.execute(httppost);

}

我刚尝试了这段代码,它返回了http 200代码。这意味着此代码可以正常工作并且应用程序连接到服务器。但是,当我想显示数据时,使用以下代码:

public JSONObject getJSONFromUrlAuth(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        Config c = new Config();
        JSONObject jo = c.getSetting();

        DefaultHttpClient httpClient = new DefaultHttpClient();
         //CredentialsProvider credProvider = new BasicCredentialsProvider();
        try {
            //credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(jo.getString("username"), jo.getString("password")));
            //httpClient.setCredentialsProvider(credProvider);
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                    new UsernamePasswordCredentials(jo.getString("username"), jo.getString("password")));

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

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

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

它未经授权返回http 401。

我的问题是为什么我的应用程序无法连接到服务器,即使我已经登录?如何解决这个问题呢?? 先谢谢

0 个答案:

没有答案