我的问题是我使用Volley将一个简单的数据发送到PHP服务器,但有时候消息不会发送。
甚至没有错误显示在我的logcat中,也具有完全的互联网访问
onClick()
功能
public void sendServer(View v){
Intent mServiceIntent = new Intent(getApplicationContext(), CapitalService.class);
mServiceIntent.putExtra("subject", "Photos");
mServiceIntent.putExtra("reciever", UserNames);
mServiceIntent.putExtra("sender", myAcc);
mServiceIntent.putExtra("Allmex",Html.fromHtml(imageName)+"");
mServiceIntent.putExtra("unique",myData);
startService(mServiceIntent);
}
capitalservice.java
public class CapitalService extends IntentService{
String country = "",Newmessage = "",subject="",reciever="",sender="",Allmex="";
String capital = "",unique="";
String url = "";
JSONObject obj;
Context context = this;
public CapitalService(){
super("Empty Constructor");
}
public CapitalService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
subject = intent.getStringExtra("subject");
reciever = intent.getStringExtra("reciever");
sender = intent.getStringExtra("sender");
Allmex = intent.getStringExtra("Allmex");
unique = intent.getStringExtra("unique");
obj = new JSONObject();
try {
obj.put("sender", sender);
obj.put("reciever",reciever);
obj.put("message",Allmex);
obj.put("subject",subject);
obj.put("id_no",unique);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
makeJsonObjectRequest();
}
private void makeJsonObjectRequest() {
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
"*****", obj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
response.getString("success");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//some error gone resend the data
Intent mServiceIntent = new Intent(getApplicationContext(), CapitalService.class);
mServiceIntent.putExtra("subject", subject);
mServiceIntent.putExtra("reciever", reciever);
mServiceIntent.putExtra("sender", sender);
mServiceIntent.putExtra("Allmex",Allmex);
mServiceIntent.putExtra("unique",unique);
mServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(mServiceIntent);
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(
0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
}