【发布时间】:2011-10-24 13:10:35
【问题描述】:
我想检查一个条件,如果为真,则应该转到下一个语句。我面临的问题是我已将代码放入 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 响应
消息:如果用户名和密码不正确
会话:如果用户名和密码正确
我想检查会话是否为真,然后通过控件初始化会话语句并将其拆分。
希望我很清楚。 提前致谢
【问题讨论】:
-
optJSONObject 改为 getJSONObject 并检查它是否返回 null developer.android.com/reference/org/json/…