0

我从事连接到 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。

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

4

0 回答 0