2011-08-04 13 views
1

정보 이메일 주소와 성을 가진 JSON을 보내려하고 {상태 : "만든"} 또는 {상태 : "다시 보내기"}라고 말하기 위해 서버의 응답을 기다리고 있습니다. 대답에 따라 팝업 메시지가 나타납니다. 상태 클래스에서 정보를 추출하는 데 내가 무엇을 사용하는지 궁금합니다. 감사! 다음은 다음 JSONObject을 생성, 문자열로 응답을 변환해야JSON 응답 받기 (안드로이드)

protected void sendJson(final String email, final String firstN, 
     final String lastN) { 
    Thread t = new Thread() { 
     public void run() { 
      Looper.prepare(); // For Preparing Message Pool for the child 
           // Thread 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 
        10000); // Timeout Limit 
      HttpResponse response; 
      JSONObject json = new JSONObject(); 
      try { 

       // post in the url 


       HttpPost post = new HttpPost(
         "https://iphone-radar.com/accounts"); 
       json.put("email_address", email); 
       json.put("first_name", firstN); 
       json.put("last_name", lastN); 
       StringEntity se = new StringEntity("JSON: " 
         + json.toString()); 
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, 
         "application/json")); 
       post.setEntity(se); 
       response = client.execute(post); 
       /* Checking response */ 
       if (response != null) { 
        String str = response.getEntity().toString(); 
        if (str.equals("Created")) { 
         new AlertDialog.Builder(CreateAccount.this) 
           .setTitle("Account Creation Successful") 
           .setMessage(
             "An activation code has been sent to you. Please check your SPAM folder if you do not receive your activation code email") 
           .setNeutralButton("OK", null).show(); 
        } else if(str.equals("Resend")) { 
         new AlertDialog.Builder(CreateAccount.this) 
           .setTitle("Code Resent") 
           .setMessage(
             "Your activation code has been resent to your email.\n\nIf you are not receiving your activation code, our email is being blocked. Please email us at '[email protected]' and we will manually send you a code.") 
           .setNeutralButton("OK", null).show(); 
        } 


       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

     } 

답변

2

을 받아 내 코드입니다. 그런 다음 JSON 객체의 속성에 액세스 할 수 있습니다. 이 시도 : 팁을위한

org.json.JSONObject obj = new org.json.JSONObject(org.apache.http.util.EntityUtils.toString(response.getEntity())); 
        if ("Created".equals(obj.getString("status"))) { 
         new AlertDialog.Builder(CreateAccount.this) 
           .setTitle("Account Creation Successful") 
           .setMessage(
             "An activation code has been sent to you. Please check your SPAM folder if you do not receive your activation code email") 
           .setNeutralButton("OK", null).show(); 
        } else if("Resend".equals(obj.getString("status"))) { 
         new AlertDialog.Builder(CreateAccount.this) 
           .setTitle("Code Resent") 
           .setMessage(
             "Your activation code has been resent to your email.\n\nIf you are not receiving your activation code, our email is being blocked. Please email us at '[email protected]' and we will manually send you a code.") 
           .setNeutralButton("OK", null).show(); 
        } 
+0

감사합니다,하지만 내가 디버거를 통해 선 org.json.JSONObject OBJ = 새로운 org.json.JSONObject을 넣을 때 (org.apache.http.util.EntityUtils.toString을 (response.getEntity())); 나는 나를 잡으려고 보낸다. (예외 e) 나 또는 서버에 문제가 있는가? 다시 한 번 감사드립니다 – Sean

+0

어떤 예외가 있습니까? 전체 예외를 로그에 출력하려면'android.util.Log.v ("EXCEPTION", "["+ e.getMessage() + "]", e);'를 사용하십시오. 응답의 엔티티가 null (아마도 서버)이거나 'EntityUtils.toString' 메소드에서 변환 오류가 있습니다. – Femi

+0

[값 java.lang.String의 값 은 JSONObject로 변환 될 수 없습니다] 이것은 잘못된 웹 사이트를 사용한다는 것을 의미합니까? 다시 한 번 감사드립니다 – Sean