Tuesday, November 6, 2012

Android HttpClient- Parse JSON

4:18 AM


JSON from server:

{"res":"success","click":{"value":[0,2,1,1,1,1,0,2,1,1,1,1,1,0,5,2,1,1,0,0,1,1,1,1,7],"gameID":"5098fac4c96d664f690079d2","turn":"winBagh","goat_count":20,"dead_goat":5,"timestamp":"1352203321097","_id":"5098fc39c96d664f69007a41"}}

try{
postParameters= new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("gameID", online_gameID));
String response = OBHttpClient.executeHttpPost("http://192.168.135.136/json", postParameters);

JSONObject resObj = new JSONObject(response);
Log.i("OB","resObj = " + resObj  );
String resServer = resObj.getString("res");
Log.i("OB","resServer = " + resServer  );

if(resServer.equals("error")){
Toast.makeText(ctx,"Wait for other user move", Toast.LENGTH_LONG).show();
    return false;
}
else{

JSONObject clickObj = resObj.getJSONObject("click");
Log.i("OB","clickObj = " + clickObj  );
String value = clickObj.getString("value").replace("[", "").replace("]", "");
String[] coord = value.split(",");
int[] orgval = new int[25];
for(int i=0; i<25 ; i++)
orgval[i] = Integer.parseInt(coord[i]);
//click.setGameValue(orgval);
click.setClicks(orgval, ((clickObj.getString("turn") == "goat")? 1 : 2), clickObj.getInt("dead_goat"), clickObj.getInt("goat_count"));//, clickObj.getString("timestamp"), clickObj.getString("gameID"));
return true;

}
}
catch(Exception e){
    Toast.makeText(ctx,"Error", Toast.LENGTH_LONG).show();
}

Written by

We are passionate programmers. Enjoying the rich platforms of Semicolon Family of Programming languages we are proud to call ourselves Semicolon Developers.

0 comments:

Post a Comment

 

© 2013 Echo "Semicolon Developers"; Kathmandu. All rights resevered @ Semicolon Developers Network Pvt. Ltd.. Designed by Templateism

Back To Top