实际上我根本没有在我的请求中使用 addHeader,我只是使用
request.setHeader("json", json.toString()); . 你认为这是问题所在吗?在我的 doInBackground 函数中,我有该代码:
JSONObject json = new JSONObject();
json.put("action", "get");
json.put("serial", mSerial);
json.put("appId", AppConstants.APP_ID);
json.put("dateFrom", MainApplication.dbHelper.getNotificationLastTime());
Calendar now = Calendar.getInstance();
json.put("currentDate",sdfyyyyMMdd.format(now.getTime()));
Log.i(TAG, "GetNotificationTask request = " + json.toString());
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(AppConstants.URL_NOTICATION);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF-8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
Log.i(TAG, "GetNotificationTask response = " + result);
JSONObject jsonRes = new JSONObject(result);
JSONObject notifications = jsonRes.getJSONObject("notifications");
String success = notifications.getString("success");
if (success.equals("1")) {
JSONArray messages = notifications.getJSONArray("0");
Log.i(TAG, "Notification messages count = " + messages.length());
String message = "";
ArrayList<NotificationData> msgArray = new ArrayList<NotificationData>();
if (messages.length() == 0)
return null;
for (int i = 0; i < messages.length(); i++) {
NotificationData info = new NotificationData();
JSONObject object = messages.getJSONObject(i);
info.setId(object.getString("msgId"));
info.setSerial(object.getString("fkmsgSerial"));
info.setTitle(object.getString("msgTitle"));
info.setMessage(object.getString("msgMessage"));
info.setDate(object.getString("msgDate"));
info.setImage(object.getString("msgImage"));
msgArray.add(info);
if (i == 0)
message = info.getMessage();
else
message = "\n" + info.getMessage();
}
MainApplication.dbHelper.insertMessages(msgArray);
String title = "";
if (messages.length() == 1)
title = "1 new notificiation";
else
title = messages.length() + " new notificiations";
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("SHOW_MESSAGES", true);
PendingIntent pIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.drawable.ic_launcher)
.setSound(soundUri)
.setContentIntent(pIntent).getNotification();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(R.string.alarm_service_started, noti);
}