2011-08-04 78 views
1

我想發送一個帶有信息電子郵件地址,名字和姓氏的JSON,並期望來自服務器的響應表示{status:「Created」}或{status:「Resend」}並根據答案會有一個彈出消息。我想知道我用什麼從狀態類中提取信息。謝謝! 這裏是我的代碼接受接收JSON響應(android)

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

你應該響應轉換成字符串,然後創建一個JSONObject。然後你可以訪問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