如何解决Volley libray 两次发布数据
我正在使用 android 应用程序上的 volley 库向 PHP API 发送发布请求。我遇到的问题是它发布了两次数据。
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String responseMessage = jsonObject.getString("message");
if(!jsonObject.getBoolean("error")){
DB.updateTransaction(msgCode,"sent");
Toast.makeText(context,responseMessage,Toast.LENGTH_SHORT).show();;
}
} catch (JSONException e) {
e.printstacktrace();
}
}
public void onReceive(Context context,Intent intent) {
// Todo: This method is called when the broadcastReceiver is receiving
// an Intent broadcast.
setConfigurationsReceiver(context);
Log.i(TAG,"Intent Received: "+intent.getAction());
if (intent.getAction()==SMS_RECEIVED){
Bundle dataBundle = intent.getExtras();
if(dataBundle != null){
//creating PDU protocol Data unit object which is a protocol for transferring message
Object[] mypdu = (Object[])dataBundle.get("pdus");
final SmsMessage[] message = new SmsMessage[mypdu.length];
for(int i =0; i< mypdu.length; i++){
//for build version >= API
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
String format = dataBundle.getString("format");
//From PDU we get all object and smsMessage using following line of code
message[i] = SmsMessage.createFromPdu((byte[])mypdu[i],format);
}else{
message[i] = SmsMessage.createFromPdu((byte[]) mypdu[i]);
}
msg += message[i].getMessageBody().toString().replace("null","");
phoneNo = message[i].getoriginatingAddress();
}
msg = msg.replace("null","");
if(phoneNo.trim().equals("mpesa")) {
dbHelper = new DBHelper(context.getApplicationContext());
String[] msgArr = msg.split(" ");
String pat = "Ksh([0-9,.]+)";
Pattern pattern = Pattern.compile(pat,Pattern.MULTILINE);
Matcher matcher = pattern.matcher(msg);
if(matcher.find()){
amount = Double.parseDouble(matcher.group(1).replace(",",""));
}
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = new Date();
msgDate = dateFormat.format(date);
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
msgTime = timeFormat.format(date);
String msgCode = msgArr[0].trim();
dbHelper.insertTransaction(msgCode,amount,"",msgDate,msgTime,msg,clientId,tillNumber);
//insert to web
volleHelper = new VolleHelper(context);
volleHelper.mpesaApi(msgCode,tillNumber);
Toast.makeText(context.getApplicationContext(),"message: " + msg + "phone: " + phoneNo,Toast.LENGTH_SHORT).show();
}
}
}
// throw new UnsupportedOperationException("Not yet implemented");
}
发布是在 volleHelper 类中名为 mpesaApi 的方法中完成的。收到短信后,我立即调用该方法。 它不会每次发布两次。互联网速度较慢时会出现此问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。