HttpURLConnection 、HttpPost、Okhttp 等POST方式发送JSON数据
来源:互联网 发布:正交矩阵的性质证明 编辑:程序博客网 时间:2024/06/07 01:30
一、HttpURLConnection Post方式发送JSON数据
public class GetResult extends AsyncTask<String, String, String> { protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... arg0) { String result = ""; URL url = null; BufferedReader reader = null; try { String parma = "{\"name\": \"hah\"}"; url = new URL("url"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Length", String.valueOf(parma.length())); connection.setRequestProperty("Content-Type","application/json; charset=UTF-8"); connection.setRequestProperty("accept","application/json"); OutputStream out = connection.getOutputStream(); out.write(parma.getBytes()); out.flush(); out.close(); if (connection.getResponseCode()==200){ reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); result = reader.readLine(); } } catch (Exception e) { e.printStackTrace(); }finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } @Override protected void onPostExecute(String re) { super.onPostExecute(re); value.setText(re); } }
2、HttpPost 发送json数据
try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("url"); StringEntity se = new StringEntity("{\"dog\":{\"name\": \"hah\"}}"); httpPost.setEntity(se); httpPost.setHeader("Content-Type", "application/json"); @SuppressWarnings("deprecation") HttpResponse httpResponse = httpClient.execute(httpPost); String str = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); re = JSON.parseObject(str,ModelArrayResult.class); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
3、Okhttp post 发送JSON
public class HttpUtil { public static final OkHttpClient client = new OkHttpClient(); private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); public static <T> T post(String Url, String postBody, Class<T> clazz) { T result = null; Request request = new Request.Builder() .url(Url) .post(RequestBody.create(MEDIA_TYPE_JSON,postBody)) .build(); Response response = null; try { response = client.newCall(request).execute();// 此处response.body().string()能用调用一次! String str = response.body().string(); Log.i("ResponseToString",str); result = JSON.parseObject(str, clazz); }catch (Exception e){ e.printStackTrace(); }finally { response.close(); } return result; }}
阅读全文
0 0
- HttpURLConnection 、HttpPost、Okhttp 等POST方式发送JSON数据
- HttpURLConnection方式、HttpClient方式发送get、post数据给服务端
- HttpClient和HttpUrlConnection实现post发送和接收json数据
- Http post方式发送json数据
- HttpClient通过Post方式发送Json数据
- HttpClient通过Post方式发送Json数据
- HttpClient通过Post方式发送Json数据
- HttpURLconnection使用POST方式提交JSON数据给服务器
- HttpURLconnection使用POST方式提交JSON数据给服务器
- HttpUrlConnection 发送POST请求数据
- HttpPost发送JSON数据中文乱码问题。
- HttpPost发送JSON数据中文乱码问题
- HttpPost发送post请求
- HttpPost发送Json
- HttpPost发送Json
- POST方式发送数据
- post方式发送数据
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- 用小米机作为调试机出现错误:Installation failed with message Failed to establish session
- NVIDIA-TX2搭建QT环境开发ROS
- java文件读写耗时比较
- vue组件使用
- LeetCode 396. Rotate Function
- HttpURLConnection 、HttpPost、Okhttp 等POST方式发送JSON数据
- python基础-管道通信(进程)、线程Condition使用
- JAVA 生成当前时间后一天零点
- Neo4j中的SQL(3)
- 修改浏览器User Agent
- 微信小程序设置控件权重
- BZOJ4818:序列计数(倍增+dp)
- 管中窥豹:一线工程师看MQTT
- 表征学习什么意思?representation learning