从流媒体URL读取JSON数据?

时间:2013-03-19 07:19:34

标签: java url streaming

我正在尝试从流媒体网址中使用JSON数据

代码段如下

   HttpClient httpclient = new DefaultHttpClient();
   HttpResponse response = httpclient.execute(new HttpGet(url));

   if(response.getEntity().isStreaming() ){
     while (true) {
       BufferedHttpEntity _entity = new BufferedHttpEntity(response.getEntity());
       InputStream _stream = _entity.getContent();
       JSONObject _interaction = new JSONObject(_stream.toString());
       System.out.println(_interaction.toString());
    }
}

请指导我在没有任何外部库的情况下使用它

提前致谢

3 个答案:

答案 0 :(得分:0)

您可以在此处使用DataContractJsonSerializer查看更多信息:http://msdn.microsoft.com/en-us/library/bb412179.aspx

答案 1 :(得分:0)

我不知道你的意思"没有外部图书馆"因为它很难没有。

谷歌模块gson(http://code.google.com/p/google-gson/)非常易于使用:

String jsonString = "your json string";
Gson gson = new Gson();
YourClass obj = gson.fromJson(jsonString, YourClass.class);

答案 2 :(得分:0)

使用Buffered Reader破解它

        HttpClient httpclient = new 
        HttpResponse response = httpclient.execute(new HttpGet(url));

        if(response.getEntity().isStreaming() ){
                HttpEntity _entity = response.getEntity();
                BufferedReader reader = new BufferedReader(new InputStreamReader(_entity.getContent(),"utf-8"),8);
                String line = null;
                while ((line = reader.readLine()) != null) {
                    System.out.println( line.toString());
                }
                _entity.getContent().close();                   
        }

这解决了我的问题。