我正在尝试向Volley
安卓库发出请求,但有时我会收到数据,有时会失败,我做错了吗?
这是我用来发出请求的代码
StringRequest strReq = new StringRequest(Request.Method.POST,
Config.EDIT_KERANJANG_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, " Response: " + response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
Config.SUCCESS = jsonObject.getString("success");
Log.e("error", "nilai sukses=" + Config.SUCCESS);
if (Config.SUCCESS.equals("1")) {
SharedPreferences.Editor editor = getSharedPreferences("CODE_RESUME_EDITK", MODE_PRIVATE).edit();
editor.putString("code_resume_editk", "1");
editor.apply();
finish();
}
else {
Log.e("error", "tidak bisa ambil data 0");
}
} catch (Exception e) {
// TODO: handle exception
Log.e("error", "tidak bisa ambil data 1");
Toast.makeText(KeranjangEditActivity.this, "Gagal menyimpan!", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(KeranjangEditActivity.this,
"Gagal menyimpan!", Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("id_pesanan", sidPesanan);
params.put("quantity", sQuantity);
params.put("harga", sHarga);
return params;
}
};
这是我用来填充列表项的自定义适配器。
public class KeranjangAdapter extends BaseAdapter {
public static final int TYPE = 2;
private Context context;
private String idUserH,idProdukH,idKeranjangH,idFileH;
private LayoutInflater inflater;
private List<KeranjangModel> keranjangModels = new ArrayList<KeranjangModel>();
private int grandTotal = 0;
SessionManager sessionManager;
NumberFormat rupiahFormat;
private ColorDrawable[] vibrantLightColorList =
{
new ColorDrawable(Color.parseColor("#ffeead")),
new ColorDrawable(Color.parseColor("#93cfb3")),
new ColorDrawable(Color.parseColor("#fd7a7a")),
new ColorDrawable(Color.parseColor("#faca5f")),
new ColorDrawable(Color.parseColor("#1ba798")),
new ColorDrawable(Color.parseColor("#6aa9ae")),
new ColorDrawable(Color.parseColor("#ffbf27")),
new ColorDrawable(Color.parseColor("#d93947"))
};
public ColorDrawable getRandomDrawbleColor() {
int idx = new Random().nextInt(vibrantLightColorList.length);
return vibrantLightColorList[idx];
}
public KeranjangAdapter(Context ctx, List<KeranjangModel> keranjangModels) {
this.context = ctx;
this.keranjangModels = keranjangModels;
}
public int getGrandTotal(){
return grandTotal;
}
@Override
public int getCount() {
return keranjangModels.size();
}
@Override
public Object getItem(int location) {
return keranjangModels.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
sessionManager = new SessionManager(context);
HashMap<String, String> user = sessionManager.getUserDetails();
idUserH = user.get( SessionManager.KEY_ID );
if (inflater == null)
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.activity_keranjang_list, null);
View vv = inflater.inflate(R.layout.activity_keranjang, parent, false);
final CheckBox checkBoxs = (CheckBox) vv.findViewById(R.id.checkAll);
parent.setDescendantFocusability(ListView.FOCUS_BLOCK_DESCENDANTS);
final ImageView thumbNail = (ImageView) convertView.findViewById(R.id.fotoProdukK);
final TextView spOrder = (TextView) convertView.findViewById(R.id.textInfoK);
final TextView hargaSatuan = (TextView) convertView.findViewById(R.id.hargaSatuanK);
final TextView harga = (TextView) convertView.findViewById(R.id.textInfoHargaK);
final TextView idKeranjang = (TextView) convertView.findViewById(R.id.idKeranjangK);
final TextView idProduk = (TextView) convertView.findViewById(R.id.idProdukK);
final TextView idFile = (TextView) convertView.findViewById(R.id.idFileK);
final TextView quantity = (TextView) convertView.findViewById(R.id.jumOrderK);
final ImageButton hapusK = (ImageButton) convertView.findViewById(R.id.hapusK);
final ImageButton editK = (ImageButton) convertView.findViewById(R.id.editK);
final TextView namaProduk = (TextView) convertView.findViewById(R.id.namaProdukK);
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkItem);
final KeranjangModel m = keranjangModels.get(position);
Picasso.with(context)
.load(m.getFotoProdukK())
.placeholder(getRandomDrawbleColor())
.into(thumbNail);
int qty = Integer.parseInt(String.valueOf(m.getQuantityK()));
int harg = Integer.parseInt(String.valueOf(m.getHargaK()));
int id = Integer.parseInt(String.valueOf(m.getIdKeranjangK()));
int idp = Integer.parseInt(String.valueOf(m.getIdProdukK()));
int idf = Integer.parseInt(String.valueOf(m.getIdFileK()));
int hs = Integer.parseInt(String.valueOf(m.getHargaSatuanK()));
rupiahFormat = NumberFormat.getInstance(Locale.GERMANY);
String rupiah = rupiahFormat.format(harg);
quantity.setText(String.valueOf(qty));
spOrder.setText(m.getSpesifikasiOrderK());
hargaSatuan.setText(String.valueOf(hs));
harga.setText(rupiah);
idKeranjang.setText(String.valueOf(id));
idProduk.setText(String.valueOf(idp));
idFile.setText(String.valueOf(idf));
namaProduk.setText(m.getNamaProdukK());
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBoxs.setChecked(false);
Toast.makeText(context, "toas", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
感谢任何帮助。
答案 0 :(得分:1)
我猜这是与超时相关的问题。您可以为要发送的请求设置RetryPolicy。使用此
strReq.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
我希望这能解决问题。