0
私はいくつかの応答を得るためにサーバーに送信する必要がある次の生データを持っています。ボレーを使用して生データを投稿する方法は?
{
"nodeId": null,
"userId": null,
"mobileNumber": "0000000",
"emailId": "[email protected]",
"userProfile": null,
"region": null,
"countryCode": "01",
"password": "[email protected]",
"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);
'mRequestBody'。あなたは今それが何を含んでいるかを示しています。 's'? – greenapps
'まだ動作していません.' ??あなたは何が起こったのか、何が起こらないのかをすばやく伝えるべきです。 – greenapps
@greenapps mRequestBodyは文字列の形の生のjsonを含んでいます。私はこのコードを実行するたびに "ヌル"のボレー例外を受け取ります。 – mrnobody