开发者

JSON object parsing into a TRY, CATCH block

开发者 https://www.devze.com 2023-04-09 02:42 出处:网络
I would like to check for a condition which if true should goto the next statement. The problem I am facing is that I\'ve put the code in a try, catch block. Every time I check for its value I need to

I would like to check for a condition which if true should goto the next statement. The problem I am facing is that I've put the code in a try, catch block. Every time I check for its value I need to initialize it, but once initialized if found untrue, as normal execution the try block exits and passes the handle to the catch block

public class HandleJSON{
UserHelper userAdapter;
private static final String TAG = "&&----HTTPClient-----**";
public static String SendHttpPost (String URL, JSONObject jsonobj) {
String regName = "";

try{    

        Log.v("Json object request is ", jsonobj.toString());
        DefaultHttpClient httpClientInstance = GetHttpClient.getHttpClientInstance();
        HttpPost httpPostRequest = new HttpPost(URL);
        Log.v(TAG,"The url is "+URL);

        StringEntity se;
        se = new StringEntity(jsonobj.toString());

        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpClientInstance.execute(httpPostRequest);
        Log.i(TAG, "HTTPRESPONSE RECIEVED" +(System.currentTimeMillis()-t) + "ms");

            String resultString = convertStreamToString(response.getEntity().getContent());
            Log.v(TAG , "The response is " +resultString);
            JSONObject jsonObj = new JSONObject(resultString);

            JSONObject sessionJson = jsonObj.getJSONObject("session");
            Log.i(TAG,"is it coming here");
            Log.i(TAG,""+sessionJson.getJSONObject(resultString).toString());
            String sessionId = sessionJson.getString("sessionid");
            String name = sessionJson.getString("name");
            JSONObject messageJson = jsonObj.getJSONObject("message");
            Log.i(TAG, "message is true log statement");
            String type_JSON = messageJson开发者_JAVA技巧.getString("sessionid");
            String content_JSON = messageJson.getString("name");
            Log.i(TAG,""+messageJson.getJSONObject(resultString).toString()+type_JSON+content_JSON);

            Log.v(TAG,"The session ID is "+sessionId);
            Log.v(TAG,"The name is "+name);
            regName = name+"-"+sessionId+"-"+URL;

} catch (Exception e){
    Log.i(TAG, "message is true log statement");
    e.printStackTrace();
}
Log.v(TAG,"before the return statement "+regName.toString().split("-"));
return regName;

}

I'll put in a bit of explanation here I will get a JSON object that I will parse it. There are two types of JSON response that I will get

message: if username & password is not correct

session: if username & password is correct

I want to check if session is true, then pass the control to initialize the session statement and split it apart.

Hope I was clear. Thanks in advance


you can use

jsonObj.has("session")

to see if the resultant message is valid. The above statement will return true if resultant json has "session" key in it. you can do the same if you want to check for message.

HTH.


Well I figured this out by putting my session code in the Catch statement. If the condition is true it will continue execution. If the condition is false it will go the catch block and execute the statements there instead of just using printstacktrace()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号