java,访问一个接口,然后得到接口的返回JSON

高先生_c 2013-11-11 05:47:34
跪求。。。例子、、、、、
...全文
38771 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yvan遗忘 2015-08-09
  • 打赏
  • 举报
回复
引用
在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));报异常 java.io.IOException: Server returned HTTP response code: 405 for URL: http://114.215.97.225/kd/user/login.json?clientPlatformType=3 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305) at com.wgh.servlet.HttpJSONTest.loadJSON(HttpJSONTest.java:26) at com.wgh.servlet.HttpJSONTest.main(HttpJSONTest.java:14) 页面不允许访问 用js确可以访问
tony4geek 2013-11-12
  • 打赏
  • 举报
回复
在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));这里直接弹出来了 报什么错?我测试楼上的是可以的。
{"geonames":[{"fcodeName":"capital of a political entity","toponymName":"Mexico City","countrycode":"MX","fcl":"P","fclName":"city, village,...","name":"Mexiko-Stadt","wikipedia":"en.wikipedia.org/wiki/Mexico_City","lng":-99.12766456604,"fcode":"PPLC","geonameId":3530597,"lat":19.428472427036,"population":12294193},{"fcodeName":"capital of a political entity","toponymName":"Manila","countrycode":"PH","fcl":"P","fclName":"city, village,...","name":"Manila","wikipedia":"en.wikipedia.org/wiki/Manila","lng":120.9822,"fcode":"PPLC","geonameId":1701668,"lat":14.6042,"population":10444527},{"fcodeName":"capital of a political entity","toponymName":"Dhaka","countrycode":"BD","fcl":"P","fclName":"city, village,...","name":"Dhaka","wikipedia":"en.wikipedia.org/wiki/Dhaka","lng":90.40743827819824,"fcode":"PPLC","geonameId":1185241,"lat":23.710395616597037,"population":10356500},{"fcodeName":"capital of a political entity","toponymName":"Seoul","countrycode":"KR","fcl":"P","fclName":"city, village,...","name":"Seoul","wikipedia":"en.wikipedia.org/wiki/Seoul","lng":126.977834701538,"fcode":"PPLC","geonameId":1835848,"lat":37.5682561388953,"population":10349312},{"fcodeName":"capital of a political entity","toponymName":"Jakarta","countrycode":"ID","fcl":"P","fclName":"city, village,...","name":"Jakarta","wikipedia":"en.wikipedia.org/wiki/Jakarta","lng":106.84513092041016,"fcode":"PPLC","geonameId":1642911,"lat":-6.214623197035775,"population":8540121},{"fcodeName":"capital of a political entity","toponymName":"Tokyo","countrycode":"JP","fcl":"P","fclName":"city, village,...","name":"Tokio","wikipedia":"en.wikipedia.org/wiki/Tokyo","lng":139.69171,"fcode":"PPLC","geonameId":1850147,"lat":35.6895,"population":8336599},{"fcodeName":"capital of a political entity","toponymName":"Taipei","countrycode":"TW","fcl":"P","fclName":"city, village,...","name":"Taipeh","wikipedia":"en.wikipedia.org/wiki/Taipei_Railway_Station","lng":121.531846,"fcode":"PPLC","geonameId":1668341,"lat":25.047763,"population":7871900},{"fcodeName":"capital of a political entity","toponymName":"Bogotá","countrycode":"CO","fcl":"P","fclName":"city, village,...","name":"Bogotá","wikipedia":"en.wikipedia.org/wiki/Bogot%C3%A1","lng":-74.08175468444824,"fcode":"PPLC","geonameId":3688689,"lat":4.609705849789108,"population":7674366},{"fcodeName":"capital of a political entity","toponymName":"Beijing","countrycode":"CN","fcl":"P","fclName":"city, village,...","name":"Peking","wikipedia":"en.wikipedia.org/wiki/Beijing","lng":116.397228240967,"fcode":"PPLC","geonameId":1816670,"lat":39.9074977414405,"population":7480601},{"fcodeName":"capital of a political entity","toponymName":"Hong Kong","countrycode":"HK","fcl":"P","fclName":"city, village,...","name":"Hong Kong","wikipedia":"en.wikipedia.org/wiki/Hong_Kong","lng":114.157691001892,"fcode":"PPLC","geonameId":1819729,"lat":22.2855225817732,"population":7012738}]}
高先生_c 2013-11-12
  • 打赏
  • 举报
回复
引用 5 楼 bylijinnan 的回复:
一个简略的例子:

package com.ljn.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author lijinnan
 * @date:2013-11-11 下午6:12:26  
 */
public class HttpJSONTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo";
        String json = loadJSON(url);
        System.out.println(json);
    }

    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                                        yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }
}

。。。刚回复错了。。。我测试了一下用我要用的借口,但是在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));这里直接弹出来了
高先生_c 2013-11-12
  • 打赏
  • 举报
回复
引用 4 楼 gao1122334 的回复:
用class类,直接控制台执行,访问网址,然后得到返回值,我这有一个C#的,要和他类似的java的代码,求例子: private string doSendMessage(string wx_username, string wx_token, string fake_id, string content) { string sURL = "http://gxyweixin.duapp.com/SendMessage?"; string param = String.Format("name={0}&password={1}&fakeid={2}&content={3}", wx_username, wx_token, fake_id, HttpUtility.UrlEncode(content)); byte[] bs = Encoding.ASCII.GetBytes(param); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sURL); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bs.Length; Stream reqStream = request.GetRequestStream(); reqStream.Write(bs, 0, bs.Length); Stream objStream = request.GetResponse().GetResponseStream(); StreamReader objReader = new StreamReader(objStream); string response_data = objReader.ReadToEnd(); request = null; textpost.Text = stringify(response_data); return stringify(response_data); }
我测试了一下用我要用的借口,但是在 BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));这里直接弹出来了
柏伦 Belen 2013-11-11
  • 打赏
  • 举报
回复
通过jsp或servlet可以轻松实现,servlet获取打印流,out.print(); 上两周写的获取网络视频源地址,输入视频播放地址就可获取视频源地址,可直接下载。 比如腾讯视频:http://v.qq.com/cover/j/jowk63wbewcbatz.html 在浏览器敲如下地址: http://60.206.40.187:8080/hynet_video/1/getVideoResource?pageUrl=http://v.qq.com/cover/j/jowk63wbewcbatz.html 返回JSON字符串:

{"resourceURL":"http://124.193.165.207/vlive.qqvideo.tc.qq.com/j0013becfin.mp4?vkey=488EFCE405EDD37E356562D686FD10F1312DED60186FA4B74CF3B87AA9E3099EA13DC7B9CB819426&br=66&platform=1&fmt=mp4&level=3","requestURL":"http://v.qq.com/cover/j/jowk63wbewcbatz.html"}
bylijinnan 2013-11-11
  • 打赏
  • 举报
回复
一个简略的例子:

package com.ljn.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author lijinnan
 * @date:2013-11-11 下午6:12:26  
 */
public class HttpJSONTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String url = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo";
        String json = loadJSON(url);
        System.out.println(json);
    }

    public static String loadJSON (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                                        yc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }
}

高先生_c 2013-11-11
  • 打赏
  • 举报
回复
用class类,直接控制台执行,访问网址,然后得到返回值,我这有一个C#的,要和他类似的java的代码,求例子: private string doSendMessage(string wx_username, string wx_token, string fake_id, string content) { string sURL = "http://gxyweixin.duapp.com/SendMessage?"; string param = String.Format("name={0}&password={1}&fakeid={2}&content={3}", wx_username, wx_token, fake_id, HttpUtility.UrlEncode(content)); byte[] bs = Encoding.ASCII.GetBytes(param); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(sURL); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bs.Length; Stream reqStream = request.GetRequestStream(); reqStream.Write(bs, 0, bs.Length); Stream objStream = request.GetResponse().GetResponseStream(); StreamReader objReader = new StreamReader(objStream); string response_data = objReader.ReadToEnd(); request = null; textpost.Text = stringify(response_data); return stringify(response_data); }
高先生_c 2013-11-11
  • 打赏
  • 举报
回复
高先生_c 2013-11-11
  • 打赏
  • 举报
回复
引用 楼主 gao1122334 的回复:
跪求。。。例子、、、、、
大哥。。。不是吧,那我访问的网址放到哪里
Defonds 2013-11-11
  • 打赏
  • 举报
回复
spring mvc 的话,这样:
	/** 
     * 测试返回JSON数据 
     * @param session 
     * @return 
     */  
    @RequestMapping(value="/test")  
    @ResponseBody  
    public Object test(HttpSession session){  
           
        return session.getAttribute("permit");  
    }  

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧