我想检查一个条件,如果为true则应该转到下一个语句。我面临的问题是我把代码放在try,catch块中。每次我检查它的值时我都需要初始化它,但是一旦初始化,如果发现不真实,正常执行,try块退出并将句柄传递给catch块
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.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;
}
我将在这里做一些解释,我将得到一个JSON对象,我将解析它。我将获得两种类型的JSON响应
消息:如果用户名&密码不正确
会话:如果用户名&密码是否正确
我想检查session是否为true,然后传递控件以初始化session语句并将其拆分。
希望我很清楚。 提前致谢
答案 0 :(得分:1)
你可以使用
jsonObj.has("session")
查看结果消息是否有效。如果结果json中包含“session”键,则上述语句将返回true。如果你想检查信息,也可以这样做。
HTH。
答案 1 :(得分:0)
我通过将我的会话代码放在Catch语句中来解决这个问题。如果条件为真,它将继续执行。如果条件为false,它将进入catch块并在那里执行语句,而不是仅使用printstacktrace()