Checking if exists subObject in JSON - android

I'm checking if an object in a JSON string exists using this:
JSONObject json = null;
try {
json = new JSONObject(myJsonString);
} catch (JSONException e) { e.printStackTrace(); }
if(json.has("myObject")) System.out.println("EXISTS");
else System.out.println("DOESN'T EXIST");
The problem appears when I attempt to check if a sub object exists. e.g:
...,"queue":{"building":{"q0":{"id":177779,...
Queue always exists and building also, but q0 is not always there. So, how can I check the existence of q0? And, is there a way to check it using the Gson library?
Thank you in advance!

You can simply give it a try and return null if the try failed. Or you can break your attempt up into little pieces to monitor where it fails.
/**
* This method will return the JSONObject q0, if it exists
* If it doesn't exist it will return NULL
*
*/
private JSONObject getQZero(JSONObject json)
{
try
{
return json.getJSONObject("queue").getJSONObject("building").getJSONObject("q0");
}
catch (JSONException e)
{
// This could be triggered either because there is no q0
// or because the JSON structure is different from what was expected.
return null;
}
}
You could also go step by step, if you want to print logs for each level;
/**
* This method will show where your jsonparsing fails.
* It will throw a JSONOException if the json is way different from what
* was expected, and otherwise it will print a log of where the parsing
* failed.
*/
private JSONObject getQZero(JSONObject json) throws JSONException
{
// Stop if no queue
if (! myObject.has("queue")
{
Log.d(TAG, "no queue!");
return null;
}
JSONObject queue = myObject.getJSONObject("queue");
// Stop if no building
if (! queue.has("building")
{
Log.d(TAG, "no building!");
return null;
}
JSONObject building = queue.getJSONObject("building")
// Stop if no q0
if (! building.has("q0"))
{
Log.d(TAG, "no q0!");
return null;
}
JSONObject q0 = building.getJSONObject("q0");
// Q0 is returned here. If the method returned earlier, it returned NULL
// You could also do nested ifs, but the indentation gets crazy
return q0;
}

Use the exceptions to your advantage
try {
JSONObject i = json. getJSONObject("q0");
// Is there do something
} catch (JSONException e) {
// Isn't there
}
http://www.json.org/javadoc/org/json/JSONObject.html#getJSONObject(java.lang.String)
JSONException - if the key is not found or if the value is not a JSONObject.

Related

Facing issue in Json parsing user profile feed

I am working on a project in which I have to parse user feed data
using Json.
Here is the url of json:
http://ourfield.affixwebsolution.com/api/feed?data={%22userId%22:210}
This is my code where I am parsing the json
public static ArrayList<FeedDataClass> parseFeedddata(String res)
{
ArrayList<FeedDataClass>arr =new ArrayList<FeedDataClass>();
FeedDataClass obj=new FeedDataClass();
JSONArray jsonArray ;
JSONObject jsonInnerObj = null;
try {
JSONObject jsonObject =new JSONObject(res);
jsonArray =jsonObject.getJSONArray("data");
Log.d("eaweaer", jsonArray.toString());
for (int i = 0; i < jsonArray.length(); i++) {
jsonInnerObj=jsonArray.getJSONObject(i);
try {
obj.strid = jsonInnerObj.getString("id");
} catch (Exception e) {
// TODO: handle exception
}
try {
JSONObject timeline = new JSONObject(jsonInnerObj.getJSONObject("timeline").toString());
obj.strusername=timeline.getString("username");
Log.d("efe", obj.strusername);
obj.strurl=timeline.getString("avatar_url");
Log.d("aeefwe", obj.strurl);
} catch (Exception e) {
// TODO: handle exception
}
try {
obj.strtext=jsonInnerObj.getString("text");
Log.d("qeeqf", obj.strtext);
} catch (Exception e) {
// TODO: handle exception
}
arr.add(obj);
}
} catch (Exception e) {
// TODO: handle exception
}
return arr;
}
now the problem is, there are total three posts in json, but everytime
I am getting last feed . also it is getting printed 3 times in UI
because loop is running perfectly. dont know what is the mistake I am
doing here.
Help me to sort out this issue, Thank in advance.
You have only one instance of obj which keeps on updating the same memory location. In the final loop cycle it gets updated with 3rd value and shows you. But yes you add it 3 times in the list so you have same object thrice.
Move
FeedDataClass obj=new FeedDataClass();
JSONObject jsonInnerObj = null;
inside for loop.

Weird JSON encoding issue on Android 4.1.2

I have an issue on Android 4.1.2 where a JSON object given to us by our REST API gets encoded weirdly when sending back.
This is the snippet of json I'm getting:
"cost":{
"amount": 0,
"currency": "GBP"
}
I'm wanting to pretty much just pass this particular snippet back the same way (modifying other parts of the json), but this is what I get on Android 4.1.2:
"cost":"{amount=0, currency=GBP}"
The function I believe is causing this weird encoding is here:
private StringEntity getEntityForRequest(final Payment payment, final PaymentDelegate delegate) {
JSONObject json = new JSONObject();
MyApplication.getContext().addApplicationInformationToJSONObject(json);
StringEntity entity = null;
try {
entity = new StringEntity(json.toString(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
payment.markAsFailed("Reservation failed, data returned not expected.");
save(payment);
if (delegate != null) {
delegate.onFailure(new MyError(MyError.DEFAULT_STATUS, MyError.DEFAULT_TYPE, "Payment error", "Error during reservation"));
}
}
return entity;
}
This is the addApplicationIformationToJSONObject function:
/**
* Adds system information to a JSON object.
*/
public void addApplicationInformationToJSONObject(JSONObject json) {
try {
try {
json.put("app_version", getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
} catch (NameNotFoundException e) {
json.put("app_version", "Unknown");
}
json.put("device", getDeviceName());
json.put("os_type", "android");
json.put("os_version", String.format("%d", Build.VERSION.SDK_INT));
json.put("device_id", Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID));
} catch (JSONException e) {
MyLog.e("MyApplication", "Error when adding system information to JSON");
}
}
What's causing this weird encoding?
How can I modify the code to avoid issues like this?
Found a solution. It seems older version interprets that cost snippet as a string rather than a JSONObject. Doing this seems to solve the issue:
ticketObject.remove("cost");
ticketObject.put("cost", new JSONObject(getCost()));

Threads, atomic boolean, synchronized design considerations for a method that has to wait for a result

My code is starting to get a bit hard to debug which leads me to believe that my design choices are not ideal. I am a novice Android programming and would love some help with streamlining the design for optimum operation.
Intro
I am writing an application that uses rfcomm interface to transfer data between a client and server device. The client needs to request certain things from the server using a specific key, then it needs to wait until the server sends the result back.
Current Design
A button press triggers a request for information from the server.
A new thread is started which performs the request.
A key which is a unique integer is converted to a byte array and sent to the server.
Thread has a while loop that is waiting for a specific boolean to flip from false to true indicating a response back from the server.
Information is received on the server side. Server uses key to identify what to do next.
server starts a thread to run some query and gets a jsonString back as a result.
Server sends jsonstring converted to byte array prepended with the same identifying key back to the client.
Client reads message, and sends the byte array to a handling method based on the identifying key.
Handling method stores jsonString to a class variable and then flips the boolean to let the other thread know that the value it was waiting on has been set.
Json string is converted to object on the client side. Something is done with that object.
This code currently correctly sends info to the server, server correctly does search and gets a valid json string result. However, the issue occurs when the server writes its results make to the client. I am getting 20 messages instead of one and none match the search key...
My questions
Am I doing things in an efficient way design wise?
Can I benefit from using synchronized keyword or and Atomic Boolean to make my code more thread safe? How would I go about implementing it?
Is there a max length for converting strings to byte array? Maybe the code is trying to break up the sending for me and that's why I'm getting 20 different results?
Relevant code
public class ClientSpokesmanClass {
private final int searchKey = 2222222; //set the key to some int.
private boolean pendingSearchResults = false;
List<Place> places = new ArrayList<Place>();
private final Handler handler = new Handler(){
#Override
public void handleMessage(Message msg){
switch(msg.what) {
...
case MESSAGE_READ:
//Message received from server
readAndDistribute(msg.arg1, msg.obj);
break;
...
}
}
};
public List<Place> getPlacesFromServer(String query){
//ask server for search results
requestSearchFromServer(query);
//just wait for them...
while (pendingSearchResults){
//just waiting
}
return places;
}
private void requestSearchFromConnectedDevice(String query) {
if (mBluetoothState == STATE_CONNECTED){
byte[] bites = new byte[4];
bites = ByteBuffer.wrap(bites).putInt(searchKey).array();
byte[] stringBytes = null;
try {
stringBytes = query.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding", e);
}
int keyLength = bites.length;
int stringLength = stringBytes.length;
byte[] combined = new byte[keyLength+stringLength];
System.arraycopy(bites, 0, combined, 0, keyLength);
System.arraycopy(stringBytes, 0, combined, keyLength, stringLength);
mBluetoothService.write(combined);
}
pendingSearchResults = true;
}
private void receiveSearchResults(byte[] bites){
String jsonString = "";
PlacesJSONParser parser = new PlacesJSONParser();
try {
jsonString = new String(bites, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding", e);
}
if (D) Log.d(TAG, "Json string we got is "+jsonString);
try {
places = parser.parse(new JSONObject(jsonString));
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e(TAG, "JSON exception", e);
}
pendingSearchResults = false;
}
/**
* Reads come here first. Then, based on the key prepended to them,
* they then go to other methods for further work.
* #param bytes
* #param buffer
*/
private synchronized void readAndDistribute(int bytes, Object buffer){
byte[] buff = (byte[]) buffer;
int key = ByteBuffer.wrap(Arrays.copyOfRange(buff, 0, 4)).getInt();
if (key == searchKey){
receiveSearchResults(Arrays.copyOfRange(buff, 4, bytes));
}else{
//do something else
}
}
}
.
public class ClientUI extends Activity {
...
onQueryTextSubmit(String query){
final String queryFinal = query;
Thread thread = new Thread(){
public void run() {
places = ClientSpokesmanClass.getPlacesFromServer(query);
doSomethingWithPlaces();
}
};
thread.start();
}
}
.
public class ServerReceive {
private searchKey = 2222222;
...
//code that handles messages, reads key, and then runs doSearchAndWriteResults()
...
private synchronized void doSearchAndWriteResults(byte[] bites){
if (D) Log.d(TAG, "+++writeSearchResults");
//Initialize query and placesString
String query = null;
String placesString;
//Convert byte array to the query string
try {
query = new String(bites, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding",e);
}
//if the string was converted successfully...
if (query != null){
//Run the places query and set the json string to placesString
if (D) Log.d(TAG, "query is "+query);
PlacesProvider placeProvider = new PlacesProvider();
placesString = placeProvider.getPlacesString(query);
}
//initialize a bite array
byte[] stringBytes = null;
try {
//convert jsonString to byte array
stringBytes = placesString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
Log.e(TAG, "unsupported encoding",e);
}
//Put the search key to a byte array. I am using this key on the client side
//to confirm that we are reading searchResults and not some other type of write.
byte[] bite = new byte[4];
bite = ByteBuffer.wrap(bite).putInt(searchKey).array();
//Get the lengths of the two byte arrays
int keyLength = bite.length;
int stringLength = stringBytes.length;
//combine the byte arrays for sending
byte[] combined = new byte[keyLength+stringLength];
System.arraycopy(bite, 0, combined, 0, keyLength);
System.arraycopy(stringBytes, 0, combined, keyLength, stringLength);
if (D) Log.d(TAG, "Actually writing things here...");
//send the byte arrrays over rfcomm
mBluetoothService.write(combined);
}
}
Take a look at https://github.com/gettyimages/connect_sdk_java. Specifically, at the test application. It performs a search using an AsyncTask and the private class notifies the UI via onPostExecute. Hopefully, this will get you further along.

Strange behaviour when receiving data

I'm developing an Android app that requires me to get some data from the server, this data is comes as JSON data, I have to receive like 7 JSON Objects, I'm using regular socket programming to get this data, and I get it by launching a thread that will wait for a data to come from the server.
I'm using the following method:
public String getServerRespons() throws JSONException {
String responseLine, server_response = null_string;
try {
input = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
} catch (IOException e) {
}
int count = 0;
boolean first = true;
try {
while (true)
{
if((responseLine = input.readLine()) == null){
break;
}
first = false;
server_response = server_response + responseLine;
//
// some processing to make sure it's a valid JSON
//
if(count == 0){ // related to the Processing Lines result
System.out.println(server_response);
return response; // when commenting that line everything is ok
}
}
} catch (IOException e) {
Login.errorMessage.setText(conn_err);
}
return null;
}
With that way i got like only two or three JSON Objects of the seven ones. BUT, when commenting the return Line and let it completes with the receiving process I got all the seven Objects efficiently and each Object is separated which makes me make sure that the processing i made to validate the JSON is going so well.
I think int count is always 0 in your example so the return statement is always hit.

Type mismatch: cannot convert from Object to JSONObject

I'm relatively new to Android development and am writing my first REST-based app. I've opted to use the Android Asynchronous HTTP Client to make things a bit easier. I'm currently just running through the main "Recommended Usage" section on that link, essentially just creating a basic static HTTP client. I'm following the code given, but changing it around to refer to a different API. Here's the code in question:
public void getFactualResults() throws JSONException {
FactualRestClient.get("q=Coffee,Los Angeles", null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(JSONArray venues) {
// Pull out the first restaurant from the returned search results
JSONObject firstVenue = venues.get(0);
String venueName = firstVenue.getString("name");
// Do something with the response
System.out.println(venueName);
}
});
}
The String venueName = firstVenue.getString("name"); line is currently throwing an error in Eclipse: "Type mismatch: cannot convert from Object to JSONObject". Why is this error occurring? I searched other threads which led me to try using getJSONObject(0) instead of get(0) but that led to further errors and Eclipse suggesting using try/catch. I haven't changed any of the code on the tutorial, save for the variable names and URL. Any thoughts/tips/advice?
Thanks so much.
EDIT:
Here is the onSuccess method, modified to include the try/catch blocks suggested. Eclipse now shows the "local variable may not have been initialized" for firstVenue here: venueName = firstVenue.getString("name"); and for venueName here: System.out.println(venueName); Even if I initialize String venueName; directly after JSONObject firstVenue; I still get the same error. Any help in resolving these would be greatly appreciated!
public void onSuccess(JSONArray venues) {
// Pull out the first restaurant from the returned search results
JSONObject firstVenue;
try {
firstVenue = venues.getJSONObject(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String venueName;
try {
venueName = firstVenue.getString("name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Do something with the response
System.out.println(venueName);
}
You can try to convert object you are getting from querying to String and then use
final JSONObject jsonObject = new JSONObject(stringresult);
I was getting same error earlier, it worked for me.
Yes, you should be using getJSONObject to ensure that the value you obtain is a JSON object. And yes, you should catch the possible JSONException which is thrown if that index in the array doesn't exist, or does not contain an object.
It'll look something like this:
JSONObject firstVenue;
try {
firstVenue = venues.get(0);
} catch (JSONException e) {
// error handling
}
convert obj to json Object:
Object obj = JSONValue.parse(inputParam);
JSONObject jsonObject = (JSONObject) obj;
The solution provided by Shail Adi only worked for me by setting the initial values of firstVenue and venueName to null. Here's my code:
JSONObject firstVenue = null;
try {
firstVenue = (JSONObject)venues.get(0);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String venueName = null;
try {
venueName = firstVenue.getString("name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Do something with the response
System.out.println(venueName);

Categories

Resources