-1

I created an HttpURLConnection between a server. It is shown below

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;
  }
  }
  }

After connecting to my server I get a response in this format, which is an HTML5 response.

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

But how do I parse this information for instance lets say I want the "text" home to be set to my text view how do I specifiy?

eli
  • 335
  • 1
  • 3
  • 18

5 Answers5

0

Did you set the output in your server side? For example:@Produces

@POST
@Produces(MediaType.APPLICATION_JSON)
public String getData() {
    ...
}
0

Your best bet based on the format in your response is to use a JSON object as in this post:

Get a JSON object from a HTTP response

Get your response as an HTTPResponse object, then create a new JSON object using

JSONObject myObject = new JSONObject(result);

Using JSON is easy. But its too much for me to re write here. Take a loog at the developer page for a good description: http://developer.android.com/reference/org/json/JSONObject.html

Community
  • 1
  • 1
Nick
  • 766
  • 4
  • 12
0

Format your string to well formated JSONString first to get response like:

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

and just create a JSONObject from your JSONString like:

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
}

Why would you even want to send malformed data from server to client? It has to have a format in my opinion.

And then you could access anything with JSONObject Android APIs (As Nick mentioned above)

0

To parse html, you can use the Jsoup library.

http://jsoup.org/

Use examples:

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

Rafael Lucena
  • 635
  • 1
  • 6
  • 9
0

You can try like this

   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();
                }
Andan H M
  • 793
  • 12
  • 18