我创建了一个HttpURLConnection但是如何解析数据呢?

时间:2016-03-18 11:43:11

标签: android json server httprequest

我在服务器之间创建了一个HttpURLConnection。如下所示

public class SimpleHTTPRequest {
public static void main(String[] args) {
  HttpURLConnection connection = null;
  OutputStreamWriter wr = null;
  BufferedReader rd  = null;
  StringBuilder sb = null;
  String line = null;


  try {
     URL serverAddress = new URL("myUrl");
      //set up out communications stuff
      connection = null;

      //Set up the initial connection
      connection = (HttpURLConnection)serverAddress.openConnection();
      connection.setRequestMethod("GET");
      connection.connect();


      //read the result from the server
      rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      sb = new StringBuilder();

      while ((line = rd.readLine()) != null)
      {
          sb.append(line + '\n');
          System.out.println(sb.toString());

      }


  } catch (MalformedURLException e) {
      e.printStackTrace();
  } catch (ProtocolException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  }
  finally
  {
      //close the connection, set all objects to null
      connection.disconnect();
      rd = null;
      sb = null;
      wr = null;
      connection = null;
  }
  }
  }

连接到我的服务器后,我得到了这种格式的响应,这是一种HTML5响应。

event: data
data: {"target":1,"data":   {"text":"Home","number":0,"id":02123421,"likes":[],"newPost":true,"created":1458300896392,"edited":1458300896392},"type":"create"}

但是我如何解析这些信息,例如我可以说我希望将“text”主页设置为我的文本视图如何指定?

5 个答案:

答案 0 :(得分:0)

您是否在服务器端设置了输出?例如:@Produces

$(window).resize(function(){
   $("#isotope").masonry().masonry("reloadItems");
});

答案 1 :(得分:0)

根据您的回复中的格式,最好的选择是使用JSON对象,如下文所示:

Get a JSON object from a HTTP response

将您的响应作为HTTPResponse对象获取,然后使用

创建一个新的JSON对象
JSONObject myObject = new JSONObject(result);

使用JSON很简单。但是我在这里重写太多了。在开发者页面上抓一个loog以获得一个好的描述:http://developer.android.com/reference/org/json/JSONObject.html

答案 2 :(得分:0)

首先将字符串格式化为格式良好的JSONString,以获得如下响应:

{data: {"event": "data", "target":1,"data":{"text":"Home","number":0,"id":02123421,"likes":[],"newPost":true,"created":1458300896392,"edited":1458300896392},"type":"create"}}

只需从JSONString创建一个JSONObject,如:

while ((line = rd.readLine()) != null)
   {
      sb.append(line);
      System.out.println(sb.toString());
   }
JSONObject data = null;
try {
   data = new JSONObject(sb.toString());
} catch (JSONException e) {
   //code in case variable line isnt well format json string
}

为什么您甚至想要将格式错误的数据从服务器发送到客户端?它必须有我认为的格式。

然后你可以使用JSONObject Android APIs访问任何内容(正如尼克上面提到的那样)

答案 3 :(得分:0)

要解析html,您可以使用Jsoup库。

http://jsoup.org/

使用示例:

http://jsoup.org/cookbook/extracting-data/example-list-links

答案 4 :(得分:0)

您可以尝试这样

   try {
                    JSONObject jsonObject = new JSONObject(sb.toString());
                    String target=jsonObject.optString("target");
                        JSONObject data=jsonObject.optJSONObject("data");
                        String text=data.optString("text");
                        int number=data.optInt("number");
                        //---
                        JSONArray likes=data.optJSONArray("likes");
                        for (int i=0;i<likes.length();i++){
                            JSONObject likeObj = likes.getJSONObject(i);
                            // If you have likes object you can pares here like
                            int rating=likeObj.optInt("rating");
                        }
                        String newPost=data.optString("newPost");
                        String created=data.optString("created");
                        String edited=data.optString("edited");
                    String type=jsonObject.optString("type");
                } catch (JSONException e) {
                    e.printStackTrace();
                }