Chinaunix首页 | 论坛 | 博客
  • 博客访问: 52351
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: Java

2016-11-15 10:11:39

wemall-mobile是基于WeMall的 app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改。分享其中关于通过Http请求获取json字符串的代码供技术员学习参考使用。

点击(此处)折叠或打开

  1. package com.inuoer.util;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;

  9. /**
  10.  *
  11.  * 通过Http请求获取json字符串
  12.  * @author heqing
  13.  *
  14.  */

  15. public class HttpUtil {

  16.     public HttpUtil() {
  17.         // TODO Auto-generated constructor stub
  18.     }

  19.     public static String getGetJsonContent(String urlStr) {
  20.         
  21.         try {// 获取HttpURLConnection连接对象
  22.             URL url = new URL(urlStr);
  23.             HttpURLConnection httpConn = (HttpURLConnection) url
  24.                     .openConnection();
  25.             // 设置连接属性
  26.             httpConn.setConnectTimeout(3000);
  27.             httpConn.setDoInput(true);
  28.             httpConn.setRequestMethod("GET");
  29.             // 获取相应码
  30.             int respCode = httpConn.getResponseCode();
  31.             if (respCode == 200) {
  32.                 return ConvertStream2Json(httpConn.getInputStream());
  33.             }
  34.         } catch (MalformedURLException e) {
  35.             // TODO Auto-generated catch block
  36.             e.printStackTrace();
  37.         } catch (IOException e) {
  38.             // TODO Auto-generated catch block
  39.             e.printStackTrace();
  40.         }
  41.         return "";
  42.     }
  43.     
  44.     public static String getPostJsonContent(String urlStr){
  45.         try {
  46.             String[] urlArr = urlStr.split("\\?");
  47.             urlStr = urlArr[0];
  48.             
  49.             URL url = new URL(urlStr);
  50.             HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
  51.     
  52.             // 因为这个是post请求,设立需要设置为true
  53.             urlConn.setDoOutput(true);
  54.             urlConn.setDoInput(true);
  55.             // 设置以POST方式
  56.             urlConn.setRequestMethod("POST");
  57.             // Post请求不能使用缓存
  58.             urlConn.setUseCaches(false);
  59.             urlConn.setInstanceFollowRedirects(true);
  60.             // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
  61.             urlConn.setRequestProperty("Contet-Type",
  62.                     "application/x-www-form-urlencoded");
  63.             // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
  64.             // 要注意的是connection.getOutputStream会隐含的进行connect。
  65.             urlConn.connect();
  66.             
  67.             // DataOutputStream流
  68.             DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());
  69.             // 要上传的参数
  70.             String content = urlArr[1];
  71.             // 将要上传的内容写入流中
  72.             out.writeBytes(content);
  73.             // 刷新、关闭
  74.             out.flush();
  75.             out.close();
  76.     
  77.             // 获取相应码
  78.             int respCode = urlConn.getResponseCode();
  79.             if (respCode == 200) {
  80.                 return ConvertStream2Json(urlConn.getInputStream());
  81.             }
  82.         } catch (Exception e) {
  83.             // TODO: handle exception
  84.         }
  85.         return null;
  86.     }

  87.     private static String ConvertStream2Json(InputStream inputStream) {
  88.         String jsonStr = "";
  89.         // ByteArrayOutputStream相当于内存输出流
  90.         ByteArrayOutputStream out = new ByteArrayOutputStream();
  91.         byte[] buffer = new byte[1024];
  92.         int len = 0;
  93.         // 将输入流转移到内存输出流中
  94.         try {
  95.             while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
  96.                 out.write(buffer, 0, len);
  97.             }
  98.             // 将内存流转换为字符串
  99.             jsonStr = new String(out.toByteArray());
  100.         } catch (IOException e) {
  101.             // TODO Auto-generated catch block
  102.             e.printStackTrace();
  103.         }
  104.         return jsonStr;
  105.     }
  106. }

wemall-mobile详情下载地址:

wemall官网地址:

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

wemall

 
 
阅读(529) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~