0
我想使用Volley發佈反饋數據。在Volley中轉換Http Post請求
我收到了他們使用的參考碼Http Request
,但我無法將調用Http Post request
。
參考代碼:
FeedBack feedBack = new FeedBack();
feedBack.message = edtMessage.getText().toString().trim();
feedBack.name = edtName.getText().toString().trim();
feedBack.stars = star;
if (dialog != null) dialog.show();
HttpRequest http = new HttpRequest(getMainActivity(), Url, BaseRestClient.RequestMethod.POST, BaseModel.class, this);
http.addParam("data", new Gson().toJson(feedBack));
http.executeAsync();
}
}
@Override
public void onSuccess(BaseModel baseModel) {
if (dialog != null && dialog.isShowing()) dialog.dismiss();
if (baseModel != null) {
Log.i("output", "onSuccess> " + new Gson().toJson(baseModel));
}
}
@Override
public void onHttpError(ResponseStatus responseStatus) {
if (dialog != null && dialog.isShowing()) dialog.dismiss();
if (responseStatus != null)
Log.i("output", "onHttpError> " + new Gson().toJson(responseStatus));
}
我嘗試使用StringRequest
但我不能夠響應如上添加BaseModel
。
StringRequest stringRequest = new StringRequest(Request.Method.POST,
Url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (dialog != null && dialog.isShowing()) dialog.dismiss();
Log.d("Response", response.toString());
Toast.makeText(getActivity(), "" + response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (dialog != null && dialog.isShowing()) dialog.dismiss();
Log.d("Error.Response", "" + error);
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("data", new Gson().toJson(feedBack));
return params;
}
};
代替String
類型響應我想BaseModel
。
請指教我該怎麼做?
謝謝。