在收到通知时,我收到了下面提到的一些数据
message
从此我想要检索
Request_id
Owner
type
String mes1 = extras.getString("chat");
我正在尝试像这样检索
{"owner":0,"request_id":"501"}
我得到了这些值
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);
}
示例:从上面我想获取所有者并请求id如何检索这些值。
这是我的代码:
`self.assertEqual( wanted_datetime,'got_datetime)`
答案 0 :(得分:8)
使用该方法后,您已收到 JSON 值,因此您应该使用 JSONObject 而不是获取字符串:
JSONObject object = new JSONObject(extras.getString("chat"));
String str_owner = object.getString("owner");
String str_request_id = object.getString("request_id");
我希望您了解 JSON 概念。
答案 1 :(得分:4)
您可以使用Gson库
从json字符串创建hashmapprivate 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);
}
答案 2 :(得分:3)
这是你的聊天对象:
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;
}
}
然后,你只需要打电话
Chat chat = Gson().fromJson(s, Chat.class);
其中s是你的字符串&#34; chat&#34; :&#34;&#34;所有者&#34;:0,&#34; request_id&#34;:&#34; 501&#34;&#34;