Servlet接收json

只需要在post方法中写入

String json = IOUtil.inputStream2String(request.getInputStream());
System.out.println("json:"+json);


public static String inputStream2String (InputStream in) throws IOException { 
StringBuffer out = new StringBuffer(); 
byte[] b = new byte[4096]; 
for (int n; (n = in.read(b)) != -1;) { 
out.append(new String(b, 0, n)); 
} 
return out.toString(); 
}

 

先获得输入流,再将输入流转换成字符串即可

 

而android段需要在post中直接传json数组,应该这样写

    private Request buildPostJsonRequest(String url, String json) {
        MediaType JSON=MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(JSON,json);

        return new Request.Builder().url(url).post(requestBody).build();
    }

 

posted @ 2016-11-23 12:41  蓝冷然  阅读(2976)  评论(0编辑  收藏  举报