【问题标题】:Error while sending HTTP GET requests using Volley in Android在 Android 中使用 Volley 发送 HTTP GET 请求时出错
【发布时间】:2017-01-19 07:57:26
【问题描述】:

我在我的 android 应用程序中使用 volley 来发出 HTTP GET 请求。我收到以下错误 -

(HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isSBSettingEnabled false
BasicNetwork.performRequest: Unexpected response code 500 for D/LOG ENTRY(21952): com.android.volley.ServerError

当我使用 REST 客户端发出相同的 HTTP 请求时,它是成功的。

我的 Volley 请求代码是 -

RequestQueue queue2 = Volley.newRequestQueue(appContext);
        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,null,
                       new Response.Listener<JSONObject>() {
                           @Override
                           public void onResponse(JSONObject response) {

                               Log.d("Response",response.toString());
                          }
                       }, new Response.ErrorListener() {
                           @Override
                           public void onErrorResponse(VolleyError error) {
                               Log.d("LOG ENTRY",error.toString());                     
                           }
                       }) {

                   @Override
                   public Map<String, String> getHeaders() throws AuthFailureError {
                       HashMap<String, String> headers = new HashMap<String, String>();
                       headers.put("clientSecret", appContext.getString(R.string.username));
                       return headers;
                   }
               };

        // Add the request to the RequestQueue.
       queue2.add(req);

请您帮忙找出错误发生的原因

【问题讨论】:

  • 您使用的链接是什么?错误 500 是内部服务器错误(错误在服务器端)。
  • 我也这么认为,但是当我在 REST API 客户端上使用相同的链接时,它可以工作,所以不确定问题是什么
  • 我发送了 10 个类似的 HTTP 请求,每次对于其中一些随机我收到此错误,而对于其他我没有。当我做 getCause() 或 getStackTrace() 它是空的
  • 你试过 JsonArrayRequest 而不是 JsonObjectRequest 吗?
  • 我做了,得到同样的错误

标签: android http android-volley


【解决方案1】:

500 错误是内部服务器错误,因此它是服务器端代码的错误。造成这种情况(在这种情况下)的原因是您超出了 API 请求限制。

在这种情况下,Volley 代码很好。

【讨论】:

    【解决方案2】:

    删除这一行:

    queue2.add(req);
    

    添加这一行:

    AppController.getInstance().addToRequestQueue(queue2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2014-08-22
      • 2017-01-27
      • 2012-06-28
      • 2016-08-09
      • 2013-06-07
      相关资源
      最近更新 更多