我正在使用Web API集成到我的应用程序中。我同样使用.net服务器API,但问题是当我使用URL传递参数时,如果API的任何参数包含空格,它将引发错误。当我传递字符串值时,由于空间原因会引发错误,如果我删除空间,则一切都非常完美。请帮忙!!!
private void getEnterLoadDetail() {
pBar.setVisibility(View.VISIBLE);
pref = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
String userName = pref.getString(KEY_USER_NAME, "username");
Log.e("TAG", "USERNAME" + userName);
tripId = txtTripId.getText().toString();
materialCode = txtMaterialCode.getText().toString();
vendorCode = txtVendorCode.getText().toString();
String url = "http://18.216.239.216:8086/api/Mobile?username="+userName+"&tripid=" + tripId + "&VehicleNo=" + vehicleNo + "&MaterialName=" + materialname + "&MaterialCode=" + materialCode + "&VendorName=" + vendorName + "&VendorCode=" + vendorCode + "&MaterialRate=" + rate + "&Remarks="+remarks+"";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response", response.toString());
try {
if (response.contains("success")) {
Toast.makeText(getActivity(), R.string.load_detail_submitted, Toast.LENGTH_LONG).show();
edtVehicleNo.setText("");
edtRate.setText("");
edtRemarks.setText("");
txtMaterialCode.setText("Material Code");
txtVendorCode.setText("Vendor Code");
spinnerVendorName.setSelection(0);
spinnerMaterialName.setSelection(0);
txtTripId.setText("Trip Id");
}
} catch (Exception e) {
e.printStackTrace();
Activity activity = getActivity();
if (activity != null && isAdded()) {
Toast.makeText(activity, R.string.some_error_occured, Toast.LENGTH_LONG).show();
}
}
pBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: " + error.getMessage());
Activity activity = getActivity();
if (activity != null && isAdded()) {
Toast.makeText(activity, R.string.some_error_occured, Toast.LENGTH_LONG).show();
pBar.setVisibility(View.GONE);
}
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//adding the string request to request queue
requestQueue.add(stringRequest);
}
-------- 材料名称,所有其他变量均包含空格,并且API引发错误。
答案 0 :(得分:0)
尝试
String url = "http://18.216.239.216:8086/api/Mobile?username="+userName+"&tripid=" + tripId + "&VehicleNo=" + vehicleNo + "&MaterialName=" + materialname + "&MaterialCode=" + materialCode + "&VendorName=" + vendorName + "&VendorCode=" + vendorCode + "&MaterialRate=" + rate + "&Remarks="+remarks+"";
String convertedUrl = URLEncoder.encode(url, "utf-8");