How to add parameters to api (http post) using okhttp library in Android

后端 未结 7 1909
不思量自难忘°
不思量自难忘° 2020-11-30 01:33

In my Android application, I am using okHttp library. How can I send parameters to the server(api) using the okhttp library? currently I am using the following code to acces

7条回答
  •  时光取名叫无心
    2020-11-30 01:42

        private final OkHttpClient client = new OkHttpClient();
    
          public void run() throws Exception {
            RequestBody formBody = new FormEncodingBuilder()
                .add("email", "Jurassic@Park.com")
                .add("tel", "90301171XX")
                .build();
            Request request = new Request.Builder()
                .url("https://en.wikipedia.org/w/index.php")
                .post(formBody)
                .build();
    
            Response response = client.newCall(request).execute();
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    
            System.out.println(response.body().string());
          }
    

提交回复
热议问题