最近、私は検索インターフェースを使いたいです。しかし、私はリクエストの本文で混乱しています。 あなたは自分のサイトで検索する必要がある場合は、文献によれば、あなたは次のように行うことができます。javaのではなくカールでこの要求を投稿するそれでは、どのようリクエストをJavaで投稿する
curl -d "keyword=android" http://gankio.herokuapp.com/search
?
私はokhttpの使用を試みました。
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
String json = "keyword=android";
RequestBody body = RequestBody.create(JSON,json);
Request request = new Request.Builder()
.url("http://gankio.herokuapp.com/search")
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
Log.d("TAG",response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();`
私はあなたの人生を簡素化するライブラリを探しています。 Idkなら何でも良いがhttps://github.com/kevinsawicki/http-request#perform-a-post-request-with-some-data-and-get-the-status-of-the-responseは1行であるコード。 – zapl
curlが動作していて、コードがhttp 400エラーになっていますか? 'curl -d'はjsonではなく' application/x-www-form-urlencoded'としてデータを送信します。 http://stackoverflow.com/questions/24233632/how-to-add-parameters-to-api-http-post-using-okhttp-library-in-androidは、OkHttpでそれを行う方法です。 – zapl
ありがとうございました。私はそれを解決する。 – CoXier