使用okhttp3执行post请求
生活随笔
收集整理的這篇文章主要介紹了
使用okhttp3执行post请求
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.導(dǎo)入依賴
| <dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.14.0</version> </dependency> |
2.代碼實現(xiàn)
| package net.ruifeng.app.cloud.service.impl;import com.google.common.collect.Maps; import okhttp3.*;import java.io.IOException; import java.util.Map;public class Demo {public static final MediaType JSON = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8");public static void main(String[] args) {try {Map<String, Object> param = Maps.newHashMap();param.put("jcgl",1);param.put("putPrice",1);param.put("total",1);param.put("transactiontype",1);param.put("valuation",1);RequestBody loginBody =RequestBody.create(JSON, com.alibaba.fastjson.JSON.toJSONString(param));Response response = httpPost("http://192.161.1.1:8888/order/saveData", loginBody);System.out.println(response);} catch (Exception e) {e.printStackTrace();}}public static Response httpPost(String url, RequestBody requestBody) throws IOException {OkHttpClient client = new OkHttpClient();Request request=new Request.Builder().url(url).post(requestBody).addHeader("Content-Type", "application/x-www-form-urlencoded").build();Response response = client.newCall(request).execute();if (response.isSuccessful()) {return response;} else {throw new IOException("Unexpected code " + response);}} } |
?
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的使用okhttp3执行post请求的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用JPA映射实体类和数据库中表时,实体
- 下一篇: service层的方法上使用@Trans