当我从我的api从POST方法转到GET方法时尝试获取我的文章时我被卡住了...我尝试更改“Request.Method.GET”但仍然无法工作..我也尝试使用GET方法来自其他帖子的凌空但我不知道如何得到我所有的“category_id”....下面是我的代码:
public void getPosts(){
if(refreshed || articleList.size() == 0) {
swipeContainer.setVisibility(View.GONE);
loadingPanel.setVisibility(View.VISIBLE);
}
String apiLink;
// if(nextUrl != null)
// apiLink = nextUrl;
// else
apiLink = api.getArticlesByCategoryAPI();
pulling = true;
final HashMap<String, String> postParams = new HashMap<String, String>();
postParams.put("secret_key", api.getSecretKey());
postParams.put("id", String.valueOf(categoryId));
RequestQueue queue = Volley.newRequestQueue(getContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, apiLink, new JSONObject(postParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("object", "test here");
try {
JSONObject resultObj = response.getJSONObject("articles");
JSONObject categoryObj = response.getJSONObject("article_category");
JSONArray articlesObj = resultObj.getJSONArray("data");
if (articlesObj.length() > 0) {
totalArticle = resultObj.getInt("total");
Log.d("totalarticle", String.valueOf(totalArticle));
articleList.clear();
for (int x = 0; x < articlesObj.length(); x++) {
if(x == 19){
break;
}
JSONObject articleObj = articlesObj.getJSONObject(x);
Article article = new Article();
Category category = new Category();
article.setId(articleObj.getInt("id"));
article.setTitle(articleObj.getString("title"));
article.setStandFirst(articleObj.getString("standfirst"));
article.setContent(articleObj.getString("content_html"));
article.setThumbnail(articleObj.getString("banner_url_thumb"));
// article.setViewCount(articleObj.getInt("view_count"));
article.setDatePublished(articleObj.getString("date_publish_web"));
article.setUrl(articleObj.getString("url"));
articleList.add(article);
Log.d("listsize", String.valueOf(articleList.size()));
adapter.notifyDataSetChanged();
category.setId(categoryObj.getInt("id"));
category.setName(categoryObj.getString("name"));
category.setStatus(categoryObj.getString("status"));
article.setCategory(category);
}
swipeContainer.setVisibility(View.VISIBLE);
loadingPanel.setVisibility(View.GONE);
if (refreshed) {
swipeContainer.setRefreshing(false);
refreshed = false;
}
totalItemCount = 0;
adapter.notifyDataSetChanged();
pulling = false;
// if(resultObj.has("next_page_url") && !resultObj.getString("next_page_url").equals("null")) {
// nextUrl = resultObj.getString("next_page_url");
// } else {
// nextUrl = null;
// }
addScroll();
} else {
// something to do if zero result
System.out.println("ZERO RESULT");
}
} catch (JSONException e) {
System.out.println("FAILED PARSE RESULT: " + e.getMessage());
Log.d("cantparse" , e.getMessage());
}
adapter.notifyDataSetChanged();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Slow network connection", Toast.LENGTH_SHORT).show();
System.out.println(error.toString());
loader.hide();
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
// params.put("Authorization", "Bearer "+token);
return params;
}
};
queue.add(jsonObjectRequest);
以下是PAW的api
答案 0 :(得分:0)
它不起作用的原因是当您发送GET请求时,您需要在实际URL中包含参数:
“http://www.yayayayyay.com/params.php?param1=blah¶m2=derp3”
对于POST请求,参数以不同的方式发送,这就是您在代码中使用此参数的原因:
final HashMap postParams = new HashMap();
如果您正在使用GET,则不需要该代码。
Here是一些很好的信息。
此外,在我看来,如果您控制服务器,并且POST正在运行,那么除非您有理由不使用它,否则没有任何问题。
答案 1 :(得分:0)
我将代码更改为
public void getPosts(){
if(refreshed || articleList.size() == 0) {
swipeContainer.setVisibility(View.GONE);
loadingPanel.setVisibility(View.VISIBLE);
}
String apiLink;
if(nextUrl != null)
apiLink = nextUrl;
else
apiLink = api.getArticlesByCategoryAPI()+categoryId;
Log.d("apilink", apiLink);
pulling = true;
// final HashMap<String, String> postParams = new HashMap<String, String>();
// postParams.put("secret_key", api.getSecretKey());
// postParams.put("id", String.valueOf(categoryId));
RequestQueue queue = Volley.newRequestQueue(getContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, apiLink, null,
new Response.Listener<JSONObject>() {
@Override
答案 2 :(得分:0)
这是最好的Volley示例(我更喜欢),使用齐射可以更好地控制GET和POST方法。
控制器类可帮助您管理 GET / POST 请求。您可以使用两行代码调用Web服务(API)。< / p>
如果你发现一些令你困惑的事情,那么你可以在这个例子中问我任何事情。
希望它会对你有所帮助。