4

I´m creating an android application that stores data in CouchDB, and I need to create a database from the android application. I need to execute the command "curl-X PUT http://user:passwd@127.0.0.1:5984/myDataBase" with java methods.

I have implemented the following functions:

public static boolean createDatabase(String hostUrl, String databaseName) {
    try {
        HttpPut httpPutRequest = new HttpPut(hostUrl + databaseName);
        JSONObject jsonResult = sendCouchRequest(httpPutRequest);

        return jsonResult.getBoolean("ok");
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

private static JSONObject sendCouchRequest(HttpUriRequest request) {
    try {
        HttpResponse httpResponse = (HttpResponse) new DefaultHttpClient().execute(request);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            InputStream instream = entity.getContent();
            String resultString = convertStreamToString(instream);
            instream.close();
            JSONObject jsonResult = new JSONObject(resultString);

            return jsonResult;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

I call the function by:

createDatabase("http://user:passwd@127.0.0.1/","myDataBase");

but there is no result. I think the problem is in user:passwd because in "admin party" mode the funcion works fine calling by:

createDatabase("http://127.0.0.1/","myDataBase");
4

2 に答える 2

4

私は同じ問題を抱えていました->ヘッダーでHTTP認証を使用する必要があります。したがって、次のヘッダー行をリクエストに追加するだけです。

private static void setHeader(HttpRequestBase request)  {
    request.setHeader("Accept", "application/json");
    request.setHeader("Content-type", "application/json");
    request.setHeader("Authorization", "Basic base64(username:password)");
}

「username:password」というフレーズを base64 でエンコードする必要があることに注意してください。これは次のようになります。

request.setHeader("Authorization", "Basic 39jdlf9udflkjJKDKeuoijdfoier");
于 2011-04-13T12:30:30.297 に答える
0

libcouch -android のこのブログ投稿をご覧ください。CouchDB を使用した Android 開発を実際にサポートする優れた機能がいくつかあります。たとえば、アプリケーションのデータベースとパスワードを自動的に作成するため、ユーザーは (必要に応じて) CouchDB を透過的に使用できます。

また、CouchDB の RPC メソッドへのアクセスを提供しているため、アプリケーションのライフサイクルから DB を開始および停止できます。

セキュリティに関しては、このスレッドでまとめました。

于 2011-04-16T16:22:20.710 に答える