0

Recently I want use a search interface.But I am confused by the request body. According to reference,when you need to search in their site,you can do like this:

curl -d "keyword=android" http://gankio.herokuapp.com/search

So how to post a this request in java rather than curl?

I have tried useing 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();`
  • I'd look for a library to simplify your life. Idk if any good but https://github.com/kevinsawicki/http-request#perform-a-post-request-with-some-data-and-get-the-status-of-the-response is 1 line of code. – zapl May 28 '16 at 06:39
  • Is curl working and your code results in a http 400 error? `curl -d` sends data as `application/x-www-form-urlencoded`, not as json. http://stackoverflow.com/questions/24233632/how-to-add-parameters-to-api-http-post-using-okhttp-library-in-android would be the way to do that with OkHttp. – zapl May 28 '16 at 06:58

3 Answers3

2

I have solved this question.

 Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder().add("keyword", "android").build();
            Request request = new Request.Builder()
                    .url("http://gankio.herokuapp.com/search")
                    .post(requestBody)
                    .build();
            try {
                Response response = client.newCall(request).execute();
                Log.d("TAG", response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
0

You can try using HttpUrlConnection from java.net This link will explain HttpUrlConnection connection process.

Community
  • 1
  • 1
Hooch
  • 487
  • 3
  • 11
0

You want to call the service using post request and form param, please use the below code:

OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
      .url("http://gankio.herokuapp.com/search?keyword=android")
      .post( RequestBody.create(MediaType.parse("application/json; charset=utf-8"), ""))
      .build();

    try {
        Response response = client.newCall(request).execute();
        System.out.println(response.toString());
        System.out.println(response.body().string());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Response response = client.newCall(request).execute();

The service that you want to call does not need any body to pass, only it needs form parameter (keyword) with value (android), but you are trying to pass the parameter using the body, and this is your mistake

Safwan Hijazi
  • 2,089
  • 2
  • 17
  • 29