我创建了一个JsonObjectRequest,用于将JSONObject发送到我的WebService。当我尝试发送这个JSONObject时,对象正在发送,但是发送3个对象而不是1.在我的数据库中,添加了3个对象。 我无法理解为什么会出现这个问题,因为我总是发送1个JSONObject但是在webservice中发送了3个对象。
我尝试使用Google Chrome的Postman,并使用Postman正常发送1个JSONObject。
可能是什么?
JsonObjectRequest
public JsonObjectRequest sendContatoToEmpresa(String nome,
String email,
String telefone,
String assunto,
String mensagem,
String idEmpresa,
final ContatoAdapter listener){
urlPost.append(WebServiceURL.getBaseWebServiceURL());
urlPost.append("/ws/contatos/contatos/add.json");
JSONObject jsObject = new JSONObject();
try {
JSONObject objs = new JSONObject();
objs.put("nome", nome);
objs.put("email", email);
objs.put("telefone", telefone);
objs.put("assunto", assunto);
objs.put("mensagem", mensagem);
objs.put("pessoa_id", idEmpresa);
jsObject.put("Contato", objs);
Log.i("JSONOBJECT->", jsObject.toString());
}catch(JSONException e){
Log.e("JSONException->", e.getLocalizedMessage());
}
Log.i("URL CONTATO EMPRESA->", urlPost.toString());
JsonObjectRequest apc = new JsonObjectRequest(Request.Method.POST,
urlPost.toString(),
jsObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
Log.i("JSOBJECT->", jsonObject.toString());
if(jsonObject.getString("status").equals("999")){
listener.isSend(true);
}else{
listener.isSend(false);
}
} catch (JSONException e) {
Log.e("JSONException->", e.getLocalizedMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.e("ERROR METHOD:", "sendContatoToEmpresa in ContatoDAO: " + volleyError.getLocalizedMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
String auth = new String(android.util.Base64.encode((BasicAuthenticationRest.USERNAME + ":" + BasicAuthenticationRest.PASSWORD).getBytes(), android.util.Base64.URL_SAFE | android.util.Base64.NO_WRAP));
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Authorization", "Basic " + auth);
return headers;
}
};
return apc;
}
活动
public Integer sendContatoToEmpresa(String nome,
String email,
String telefone,
String assunto,
String mensagem,
String idEmpresa){
final int[] send = {0};
if(nome.length() == 0 ||
email.length() == 0 ||
telefone.length() == 0 ||
assunto.length() == 0 ||
mensagem.length() == 0){
Toast.makeText(getView().getContext(), "Informe todos os campos", Toast.LENGTH_SHORT).show();
send[0] = 0;
}else{
final ProgressDialog progress = new CustomProgressDialog().getCustomProgress(null, getView().getContext());
progress.show();
JsonObjectRequest app = new ContatoDAO().sendContatoToEmpresa(nome,
email,
telefone,
assunto,
mensagem,
idEmpresa,
new ContatoAdapter(){
@Override
public void isSend(Boolean value) {
if(value){
send[0] = 1;
}
progress.dismiss();
}
});
CustomVolleySingleton.getInstance(getView().getContext()).addToRequestQueue(app);
}
return send[0];
}
CustomVolleySingleton
public class CustomVolleySingleton extends Application{
private static CustomVolleySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
public static final String TAG = "VolleyPatterns";
private CustomVolleySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized CustomVolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new CustomVolleySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
答案 0 :(得分:1)
尝试执行以下操作:
app.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
您可以在此处更改超时(MY_SOCKET_TIMEOUT_MS)并更改尝试次数(DefaultRetryPolicy.DEFAULT_MAX_RETRIES)