0

Web サービスに接続する Android プロジェクトに取り組んでいます。ユーザーログイン後にデータを表示する機能があります。ログイン関数は、http ポスト メソッドを使用して 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