我是AWS的新手,我正在寻找一种推荐的替代方法,而不是使用Volley将某些内容发布到AWS端点。当前代码,我正在尝试发表评论:
public void submitComment(final Comments comment,
final RequestListener submissionListener) throws JSONException {
String url = this.baseURLPath + "topics/topicId/comments";
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();
Gson googleGSON = gsonBuilder.create();
googleGSON.toJson(comment);
JSONObject jObject = new JSONObject(googleGSON.toJson(comment));
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.POST, url, jObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
submissionListener.onSuccess();
}
},
new Response.ErrorListener() {
@Override
// Handles errors that occur due to Volley
public void onErrorResponse(VolleyError error) {
Log.e("submitComment", "(onErrorResponse) ERROR HAPPENED: " + error.toString());
submissionListener.onFailure();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("Authorization", userPreferences.getToken(context));
//params.put("Accept-Language", "fr");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(obreq);
}
答案 0 :(得分:0)
没有什么可以阻止您使用Retrofit。它可以与您已经在使用的Gson
一起使用,并且一旦设置便非常易于使用。您还可以使用它来处理基于令牌的身份验证。
例如this article将为您提供翻新的绝佳概述,并教您如何使用它。