0

I create android apps as client and node as server, i got problem when i request value from android to node, i use this code in android to communicate with node js

String xResult = getRequestJSON("http://mydomain.com:8888");
public String getRequestJSON(String Url){

       String sret="";
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(Url);
        try{
          HttpResponse response = client.execute(request);
          sret =requestJSON(response);

        }catch(Exception ex){
        }
        return sret;
    }


    public static String requestJSON(HttpResponse response){
        String result = "";
        try{
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder str = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null){
                str.append(line + "\n");
            }
            in.close();
            result = str.toString();
        }catch(Exception ex){
            result = "Error";
        }
        return result; 
    }

and i got result like this in node.

[{"posid":"P0S6f50b314b2c279a2083cb0ef821ccb4d20140218120720","id_a":"ltv@ltv.com","gambar_a":"6f50b314b2c279a2083cb0ef821ccb4d.jpg","user":"lutfi soe","pwaktu":"2014-02-18T05:07:20.000Z","posnya":"test dr android","plat":-7.983757710988161,"plong":112.6549243927002,"pjenis":"I","vote":0}]

my question is,how i receive json like that in android ?and parse to string?

thanks

ltvie
  • 931
  • 4
  • 19
  • 48

2 Answers2

0

If you are receiving Json string in proper format then you can use JSON jar to parse this json.

You can get a tutorial for JSON parsing here

Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
0

I think your question is more on how to parse and access the result in java[read Android].

Here is a solution that could help JavaScript type arrays in JAVA

JsonArray yourArray = new JsonParser()
                      .parse("[[\"2012-14-03\", 2]]")
                      .getAsJsonArray();
// Access your array like so - yourArray.get(0).getAsString();
// yourArray.get(0).getAsInt() etc

The above is using a library called Gson

P.S: I just plagiarized my own answer. Not sure what criteria to use to mark this question as a duplicate

Community
  • 1
  • 1
aravind
  • 2,867
  • 17
  • 28