我正在一個Android應用程序,其中用戶可以發送「推薦」給其他用戶。在啓動時我運行下面的方法,該方法查詢遠程數據庫,並檢查新推介:字符串我放入演員是不是我得到的字符串
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
String username = extras.getString(WeShouldActivity.ACCOUNT_NAME);
HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user_email", username));
String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
HttpGet httpget = new HttpGet("http://23.23.237.174/check-referrals?"+paramString);
JSONObject resp = new JSONObject();
JSONArray data = new JSONArray();
try {
HttpResponse response = httpclient.execute(httpget);
InputStream is = response.getEntity().getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=0;
while(i != -1){
i = is.read();
baos.write(i);
}
byte[] buf = baos.toByteArray();
//is.read(buf);
Log.v("REFERRAL RESPONSE", new String(buf));
resp = new JSONObject(new String(buf));
data = resp.getJSONArray("referrals");
Log.v("GETREFERRALSSERVICE", "Checking for new referrals");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.v("GETREFERRALSSERVICE", e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("GETREFERRALSSERVICE", e.getMessage());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("GET REFFERAL SERVICE", "JSON EXCEPTION "+e.getMessage());
}
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) getSystemService(ns);
int icon = R.drawable.restaurant;
String tickerText = "New referrals!";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "New referrals!";
Log.v("REFERRAL DATA", data.toString());
if(data.length() >0){
CharSequence contentText = "You have "+data.length()+" new referrals awaiting your approval.";
Intent notificationIntent = new Intent(this, ApproveReferral.class);
notificationIntent.putExtra("we.should.communication.data", data.toString());
//Log.v("AFTER EXTRAS INSERT", notificationIntent.getStringExtra("data"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;
nm.notify(ActivityKey.NEW_REFERRAL.ordinal(), notification);
}
}
所以,如果有對遠程數據庫的任何請示,通知將彈出。點擊該通知觸發ApproveReferral活動,如下圖所示:
@Override
public void onCreate(Bundle savedInstanceState) {
final Context c = this.getApplicationContext(); //I don't know if this is the right way to do this!
super.onCreate(savedInstanceState);
setContentView(R.layout.approve_referral);
ListView lv = (ListView) findViewById(R.id.referralList);
final List<Referral> list = new ArrayList<Referral>();
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String dataAsString = bundle.getString("we.should.communication.data");
Log.v("DATA EXTRA", dataAsString);
JSONArray data = new JSONArray();
//get referral items from extras
try {
data = new JSONArray(dataAsString);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i=0; i<data.length(); i++){
try {
JSONObject o = data.getJSONObject(i);
Log.v("REFFERAL OBJECT DATA", o.toString());
JSONObject d = new JSONObject(o.getString("data"));
list.add(new Referral(o.getString("item_name"), o.getString("referred_by"), false, d));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v("REFERRAL OBJECT DATA", e.getMessage());
}
}
Log.v("REFERRAL LIST", list.toString());
mAdapter = new ReferralAdapter(this, list);
lv.setAdapter(mAdapter);
mSave = (Button) findViewById(R.id.save);
mSave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List<Referral> approvedList = mAdapter.getApprovedList();
//TODO: send approved/rejected to remote db and delete
Referrals refs = Referrals.getReferralCategory(c);
for(Referral r: approvedList){
ReferralItem ref = refs.newItem(r.getData());
try{
ref.save();
} catch(Exception e) {
Toast.makeText(c, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
}
deleteRefs(list);
finish();
}
});
}
我一直有,我會送我一個轉診的問題(這是允許的,所以它不是),否則別人會送我一個推薦,我會得到一個通知,說,例如「你有3個新的推介」,但是當我打開審批推薦視圖,我只會得到2.
我已經追溯到這個事實上,我將其放入ApproveReferral附加內容中的「數據」字符串不是出來的字符串。
E.g.
Log.v("REFERRAL DATA", data.toString());
notificationIntent.putExtra("we.should.communication.data", data.toString());
和data.toString()看起來像:
05-28 12:53:10.874: V/REFERRAL DATA(13610): [{"data":"{\"website:0:1\":\"http:\\\/\\\/m.imdb.com\\\/title\\\/tt0848228\",\"name:0:0\":\"The Avengers\",\"comment:1:5\":\"2012\",\"rating:3:4\":\"5.0\",\"tags:1:6\":\"[{\\\"id\\\":2,\\\"color\\\":\\\"Red\\\",\\\"tag\\\":\\\"again\\\"}]\"}","item_name":"The Avengers","referred_by":"[email protected]"},{"data":"{\"website:0:1\":\"http:\\\/\\\/www.edgewaterhotel.com\\\/edgewater_dining.aspx\",\"name:0:0\":\"Six Seven\",\"comment:1:5\":\"\",\"phone number:2:2\":\"(206) 269-4575\",\"rating:3:4\":\"\",\"tags:1:6\":\"[]\",\"address:1:3\":\"{\\\"long\\\":-122.352644,\\\"address\\\":\\\"2411 Alaskan Way, Seattle, WA, United States\\\",\\\"lat\\\":47.612579}\"}","item_name":"Six Seven","referred_by":"[email protected]"},{"data":"{\"website:0:1\":\"http:\\\/\\\/www.paseoseattle.com\\\/\",\"name:0:0\":\"Paseo\",\"comment:1:5\":\"\",\"phone number:2:2\":\"(206) 545-7440\",\"rating:3:4\":\"5.0\",\"tags:1:6\":\"[{\\\"id\\\":1,\\\"color\\\":\\\"Purple\\\",\\\"tag\\\":\\\"Colin\\\"}]\",\"address:1:3\":\"{\\\"long\\\":-122.3501502,\\\"address\\\":\\\"4225 Fremont Avenue North, Seattle, WA, United States\\\",\\\"lat\\\":47.6586176}\"}","item_name":"Paseo","referred_by":"[email protected]"}]
看到,有3個項目!
,然後在approveReferral的開頭:
05-28 12:53:13.877: V/DATA EXTRA(13610): [{"data":"{\"website:0:1\":\"http:\\\/\\\/m.imdb.com\\\/title\\\/tt0848228\",\"name:0:0\":\"The Avengers\",\"comment:1:5\":\"2012\",\"rating:3:4\":\"5.0\",\"tags:1:6\":\"[{\\\"id\\\":2,\\\"color\\\":\\\"Red\\\",\\\"tag\\\":\\\"again\\\"}]\"}","item_name":"The Avengers","referred_by":"[email protected]"},{"data":"{\"website:0:1\":\"http:\\\/\\\/www.edgewaterhotel.com\\\/edgewater_dining.aspx\",\"name:0:0\":\"Six Seven\",\"comment:1:5\":\"\",\"phone number:2:2\":\"(206) 269-4575\",\"rating:3:4\":\"\",\"tags:1:6\":\"[]\",\"address:1:3\":\"{\\\"long\\\":-122.352644,\\\"address\\\":\\\"2411 Alaskan Way, Seattle, WA, United States\\\",\\\"lat\\\":47.612579}\"}","item_name":"Six Seven","referred_by":"[email protected]"}]
現在只有2
,我能想到的唯一的事情就是approveReferral,出於某種原因,是「緩存」(我把它放在引號中,因爲我不熟悉android,也不知道這個術語是多麼適用)其額外功能,並且不使用更新的額外功能。
我該如何解決這個問題?
您是否嘗試過發送2個值而不是3個來檢查是否縮減爲1或4,並且收到的字符串有3個元素,或者可能是不同的字符串,並檢查您是否接收到完全相同的字符串貼? –
我有這個問題發生在幾種不同的變化 - 2,我只得到1,4,我只得到2,他們唯一的共同點是,有一次,他們*正確 - 這就像更新只是沒有完成。 – Colleen
[與BroadcastIntents結合的通知陳舊,如何刷新它們的可能的重複?](http://stackoverflow.com/questions/7910190/notifications-combined-with-broadcastintents-are-stale-how-to-freshen-them ) – CommonsWare