2017-01-09 21 views
7

khi nhận được thông báo tôi nhận được một số dữ liệu đề cập dưới đâyLấy thông tin từ JSON bó

Bundle[{google.sent_time=1233212221545, msg={"message":"You received a chat message","type":"Match Maker","request_id":"501"}, chat={"owner":0,"request_id":"501"}, from=148566781839, google.message_id=0:148354545211779676%c3fc9c6ff9fd7ecd, android.support.content.wakelockid=3, collapse_key=do_not_collapse}] 

Từ này tôi muốn lấy

  1. message
  2. Request_id
  3. Owner
  4. type

Tôi cố gắng để lấy lại như thế này

String mes1 = extras.getString("chat"); 

Tôi nhận được những giá trị

{"owner":0,"request_id":"501"} 

Ví dụ: từ trên, chúng tôi muốn có được chủ sở hữu và yêu cầu id làm thế nào để lấy các giá trị .

Đây là mã của tôi:

public class GCMIntentService extends IntentService { 
    public static final int NOTIFICATION_ID = 1; 
    private static final String TAG = "GcmIntentService"; 
    UserLocalStore userLocalStore; 
    private NotificationManager mNotificationManager; 

    public GCMIntentService() { 
     super("GcmIntentService"); 
    } 

    Intent notificationIntent; 
    String type,token,requestid,notify_message; 
    Context context; 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     userLocalStore = new UserLocalStore(this); 
     Bundle extras = intent.getExtras(); 

     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 

     // The getMessageType() intent parameter must be the intent you received 
     // in your BroadcastReceiver. 
     String messageType = gcm.getMessageType(intent); 

     if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
      /* 
      * Filter messages based on message type. Since it is likely that GCM 
      * will be extended in the future with new message types, just ignore 
      * any message types you're not interested in, or that you don't 
      * recognize. 
      */ 

      if (GoogleCloudMessaging. 
        MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
       sendNotification("Send error: " + extras.toString()); 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_DELETED.equals(messageType)) { 
       sendNotification("Deleted messages on server: " + 
         extras.toString()); 
       // If it's a regular GCM message, do some work. 
      } else if (GoogleCloudMessaging. 
        MESSAGE_TYPE_MESSAGE.equals(messageType)) { 

       // Post notification of received message. 
       Notifyvalue msgvalues = new Notifyvalue(); 
       Log.i(TAG, "Received: " + extras.toString()); 

       String mes1 = extras.getString("chat"); 

       type = extras.getString("type"); 
       notify_message = extras.getString("msg"); 
       Global.notification_type = type; 

       if (authenticate() == true || userLocalStore.getFBLoggedInUser() == true) { 
        if(Global.notification_type.equals("Match Maker")) 
        { 
         notificationIntent = new Intent(this, Chatactivity.class); 
        } 
        else 
        { 
         notificationIntent = new Intent(this, MainActivity.class); 
        } 
       } 
       else 
       { 
        notificationIntent = new Intent(this, LoginActivity.class); 
       } 

       switch (type) { 
        case "Rating": 
         Global.notify_message = notify_message; 
         requestid = extras.getString("request_id"); 
         Global.Request_id = requestid; 

         break; 

        case "Request": 

         Global.matchtoken = extras.getString("token"); 
         requestid = extras.getString("request_id"); 
         Global.Request_id = requestid; 
         Global.notify_message = notify_message; 

         break; 

        case "Accept": 

         Global.matchtoken = extras.getString("matchtoken"); 
         requestid = extras.getString("request_id"); 
         Global.Request_id = requestid; 
         Global.notification_type = type; 
         Global.notify_message = notify_message; 

         break; 

        case "Decline": 

         Global.matchtoken = extras.getString("matchtoken"); 
         requestid = extras.getString("request_id"); 
         Global.Request_id = requestid; 
         Global.notification_type = type; 
         Global.notify_message = notify_message; 

         break; 

        case "Match Maker": 

         //requestid = extras.getString("request_id"); 
         //Global.Request_id = requestid; 
         Global.notification_type = type; 
         Global.notify_message = notify_message; 
         break; 

        case "Anonymous Rating": 

         requestid = extras.getString("request_id"); 
         Global.Request_id = requestid; 
         Global.notification_type = type; 
         Global.notify_message = notify_message; 
         Global.feedback = extras.getString("feedback"); 
         Global.ratestatus = extras.getString("ratestatus"); 

         break; 
        default: 

         break; 
       } 

       Log.i(TAG, "message: " + Global.notify_message); 
       sendNotification(extras.getString("message")); 

      } 
     } 

       // Release the wake lock provided by the WakefulBroadcastReceiver. 
       GcmBroadcastReceiver.completeWakefulIntent(intent); 
    } 
+0

sử dụng json parseing –

+0

Tôi mới sử dụng android có thể bạn chỉ giải thích với một số mã hóa – VyTcdc

+0

googling cho rằng –

Trả lời

8

Sau khi sử dụng phương pháp mà bạn đã nhận được một giá trị JSON nên thay vì nhận được trong chuỗi bạn nên sử dụng JSONObject như dưới đây:

JSONObject object = new JSONObject(extras.getString("chat")); 
String str_owner = object.getString("owner"); 
String str_request_id = object.getString("request_id"); 

Tôi hy vọng bạn hiểu về khái niệm JSON.

+0

nhờ Ok tôi sẽ kiểm tra tại – VyTcdc

+0

Bắt Unhandled ngoại lệ org.json.Jsonexception – VyTcdc

+3

try {// JSON parcing } catch (JSONException e) { // TODO Khối catch được tạo tự động e.printStackTrace(); // AlertDialog ("Lỗi máy chủ! Vui lòng thử lại."); } – bharat7777

3

Đây là trò chuyện Object của bạn:

public class Chat { 

@SerializedName("owner") 
@Expose 
private Integer owner; 
@SerializedName("request_id") 
@Expose 
private String requestId; 

public Integer getOwner() { 
return owner; 
} 

public void setOwner(Integer owner) { 
this.owner = owner; 
} 

public String getRequestId() { 
return requestId; 
} 

public void setRequestId(String requestId) { 
this.requestId = requestId; 
} 

} 

Sau đó, bạn chỉ cần gọi

Chat chat = Gson().fromJson(s, Chat.class); 

đó s là chuỗi của bạn "chat": "" chủ sở hữu ": 0," REQUEST_ID" : "501""

4

Bạn có thể tạo hashmap từ chuỗi json sử dụng Gson thư viện

private void testing(){ 
    String s = "{\"owner\":0,\"request_id\":\"501\"}"; 
    Gson gson = new Gson(); 
    HashMap<String, String> map = (HashMap<String, String>) gson.fromJson(s, HashMap.class); 
    System.out.println("testing values::"+map); 
} 
Các vấn đề liên quan