2011-09-29 2 views
1

참이면 다음 문으로 이동해야하는 조건을 확인하고 싶습니다. 내가 직면하고있는 문제는 try, catch 블록에 코드를 넣었다는 것이다. 사실이 발견하면 정상적인 실행으로, try 블록 종료를 초기화 catch 블록TRY, CATCH 블록으로 JSON 객체 구문 분석

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 응답의 두 가지 유형이 있습니다

메시지 : 사용자 이름 & 암호가 정확하지 않은 경우

세션 : 세션이 다음에 해당하는 경우 사용자 이름 & 입력 난 검사 할

맞다면 컨트롤을 전달하여 세션 문을 초기화하고 분리하십시오.

희망이 분명했습니다. 사전

+1

getJSONObject 대신 optJSONObject를 사용하고 null을 반환하는지 확인합니다. http://developer.android.com/reference/org/json /JSONObject.html#optJSONObject%28java.lang.String%29 – Selvin

답변

0

글쎄, 나는 이것을 캐치 (Catch) 문장에 넣어서 알아 냈다. 조건이 true이면 실행을 계속합니다. 조건이 false이면 catch 블록으로 가서 printstacktrace()를 사용하는 대신 문을 실행합니다.

1

에서 덕분에 당신은 결과 메시지가 유효한지 확인

jsonObj.has("session") 

를 사용할 수 있습니다. 결과 json에 "session"키가 있으면 위의 명령문은 true를 반환합니다. 메시지를 확인하려는 경우에도 동일한 작업을 수행 할 수 있습니다.

HTH.