0

サーバー上の php ファイルは、htaccess でパスワード保護されています。Android アプリからこれらのファイルをリクエストするにはどうすればよいですか?

Google検索で関連する回答が見つかりませんでした。

4

2 に答える 2

1

ここで答えを見つけることができます:

Android での基本的な HTTP 認証

基本的:

HttpUriRequest request = new HttpGet(YOUR_URL); // Or HttpPost(), depends on your needs
String credentials = YOUR_USERNAME + ":" + YOUR_PASSWORD;
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", "Basic " + base64EncodedCredentials);

HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(request);
// You'll need to handle the exceptions thrown by execute()

最後の行を次のように置き換えることができます。

編集:

次のようなことを試すことができます:

try {
HttpResponse response = httpclient.execute(request);
    //this is the login response, logged so you can see it - just use the second part of the log for anything you want to do with the data

    Log.d("Login: Response", EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
//if something went badly wrong
}

応答を表示できます。問題は JSON の解析にある可能性があります。

于 2013-09-23T13:30:03.070 に答える