java发起http请求获取返回的Json对象方法
2018-03-30 08:48
906 查看
话不多说,先看代码!
/** * Created by david on 2017-7-5. */ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class HttpRequestUtil { /** * 发起http请求并获取结果 * @param requestUrl 请求地址 */ public static JsonObject getXpath(String requestUrl){ String res=""; JsonObject object = null; StringBuffer buffer = new StringBuffer(); try{ URL url = new URL(requestUrl); HttpURLConnection urlCon= (HttpURLConnection)url.openConnection(); if(200==urlCon.getResponseCode()){ InputStream is = urlCon.getInputStream(); InputStreamReader isr = new InputStreamReader(is,"utf-8"); BufferedReader br = new BufferedReader(isr); String str = null; while((str = br.readLine())!=null){ buffer.append(str); } br.close(); isr.close(); is.close(); res = buffer.toString(); JsonParser parse =new JsonParser(); object = (JsonObject) parse.parse(res); } }catch(IOException e){ e.printStackTrace(); } return object; } public static JsonObject postDownloadJson(String path,String post){ URL url = null; try { url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 // conn.setConnectTimeout(10000);//连接超时 单位毫秒 // conn.setReadTimeout(2000);//读取超时 单位毫秒 // 发送POST请求必须设置如下两行 httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); // 发送请求参数 printWriter.write(post);//post的参数 xx=xx&yy=yy // flush输出流的缓冲 printWriter.flush(); //开始获取数据 BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; byte[] arr = new byte[1024]; while((len=bis.read(arr))!= -1){ bos.write(arr,0,len); bos.flush(); } bos.close(); JsonParser parse = new JsonParser(); return (JsonObject)parse.parse(bos.toString("utf-8")); } catch (Exception e) { e.printStackTrace(); } return null; } //测试 public static void main(String args [] ) { JsonObject res = null; // res = getXpath("http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42"); res = postDownloadJson("http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42","123"); System.out.println(res); System.out.println(res.get("code")); System.out.println(res.get("data")); } }
看第一个方法,发送get请求获取后台数据,其中,将返回回来的字符串解析成json对象用到了google的Gson.jar包,用到了其中JsonParser的parse方法。
第二个方法,发送post数据到后台并获取后台数据。
以上这篇java发起http请求获取返回的Json对象方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- java发起http请求获取返回的Json对象
- HTTP请求数据返回解析Josn org.codehaus.jackson.map.JsonMappingException的解决方法
- Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- Ext发起ajax请求获取返回对象属性值
- Java获取http和https协议返回的json数据
- Java获取http和https协议返回的json数据
- 微信相关 -- 可以通过调用下面的方法 请求对应的url 获取微信服务器返回的json数据
- httpclient发起https请求以及获取https返回内容
- AndroidAsyncHttp与Gson或者Json组合使用,请求直接返回解析对象
- 利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串
- Java用org.apache.http.client的HttpClient发送Post请求 可获取返回Header
- 通过请求不同的url返回Json对象并在前端显示的解决方法
- Ajax核心XMLHttpRequest对象、(发送请求、接收)方法和属性介绍、AJAX开发框架、数据格式提要(XML、JSON、HTML)
- django通过ajax发起请求返回JSON格式数据的方法
- JQuery中get和getJSON方法返回json对象无法获取问题
- Java获取http和https协议返回的json数据
- Http请求返回json解析java.lang.IndexOutOfBoundException错误