我有以下原始数据,我需要将其发送到服务器以获得一些响应。
{
"nodeId": null,
"userId": null,
"mobileNumber": "0000000",
"emailId": "xxx@gmail.com",
"userProfile": null,
"region": null,
"countryCode": "01",
"password": "pass@123",
"places": [],
"trustedNetwork": [],
"profilePic": null,
"fullName": null,
"longitude": 0.0,
"latitude": 0.0 }
在发布原始数据时,我还必须发送身份验证标头参数。无论我尝试什么,我都无法发布它。
我使用了以下尚未使用的代码
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
String URL = baseURL;
final String mRequestBody = s;
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
new AlertDialog.Builder(getActivity())
.setTitle("hmm")
.setMessage(""+response)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.getLocalizedMessage());
new AlertDialog.Builder(getActivity())
.setTitle("SORRY")
.setMessage(""+error.getLocalizedMessage())
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
mRequestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
String creds = String.format("%s:%s", "xxxxxx", "xxxxxxxxxxxxxx");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
headers.put("Authorization", auth);
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
requestQueue.add(stringRequest);
答案 0 :(得分:0)
这里,在创建引发凌空异常情况的授权时实际发生了问题。因为,我使用的密码非常长Base64.default没有正确处理它。将其更改为Base64.no_wrap后,问题就解决了