【问题标题】:Android Volley Post ErrorAndroid Volley 发布错误
【发布时间】:2018-12-17 23:44:40
【问题描述】:

我有一个错误。我将数据发送到服务器,但此代码不起作用。 请帮助我。谢谢。我有简单的登录方法。那行得通,但这行不通。错误在哪里。

@覆盖 protected String doInBackground(String... params) {

        StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, new AppData().MUSTERI_KAYIT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObj = null;
                            Log.i(" ADD CUSTOMER RESPONSE - ", response);
                        } catch (Exception e) {
                            Log.i(" ADD CUSTOMER RESPONSE ERROR ", e.toString());
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("volley", "Error: " + error.getMessage());
                error.printStackTrace();
                snackBar();
                Log.e(" --- ", error.toString());
            }
        }) {

            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
                    VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
                    volleyError = error;
                    Log.d(" Errrrorrrrr - ", volleyError.toString());
                }
                return volleyError;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                String auth = "Bearer " + access_token;
                params.put("Authorization", auth);
                params.put("Content-Type","application/x-www-form-urlencoded");

                params.put("accounttype", editMusteriTipi.getText().toString());
                params.put("title", editUnvan.getText().toString());

                params.put("phone", editPhone.getText().toString());
                params.put("email", editEMail.getText().toString());
                params.put("tckn", editTcKimlikNo.getText().toString());

                params.put("city", editCity.getText().toString());
                params.put("district", editIlce.getText().toString());
                params.put("addressdetail", editAdress.getText().toString());
                //params.put("Vinno", editAracNo.getText().toString());
                for (int i = 0; i < sektorList.size(); i++) {
                    if (sektorList.get(i).getName().equals(editSektor.getText().toString())) {
                        params.put("sector", sektorList.get(i).getId());
                    }
                }
                for (int i = 0; i < kategoriList.size(); i++) {
                    if (kategoriList.get(i).getName().equals(editKategori.getText().toString())) {
                        params.put("category", kategoriList.get(i).getId());
                    }
                }
                for (int i = 0; i < vergiDairesiList.size(); i++) {
                    if (vergiDairesiList.get(i).getName().equals(editVergiDairesi.getText().toString())) {

                    }
                }
                params.put("vd", editVergiDairesi.getText().toString());
                params.put("vdno", editVergiDairesiNo.getText().toString());
                return params;
            }

        };

        RequestQueue queue = Volley.newRequestQueue(CustomerRecord.this);
        queue.add(jsonObjRequest);
        return null;
    }

下面的代码是错误的。

E/ ---: com.android.volley.VolleyError: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
    <title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
    <style type="text/css">
    <!--
    body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
    fieldset{padding:0 15px 10px 15px;} 
    h1{font-size:2.4em;margin:0;color:#FFF;}
    h2{font-size:1.7em;margin:0;color:#CC0000;} 
    h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
    #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
    background-color:#555555;}
    #content{margin:0 0 0 2%;position:relative;}
    .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
    -->
    </style>
    </head>
    <body>
    <div id="header"><h1>Server Error</h1></div>
    <div id="content">
     <div class="content-container"><fieldset>
      <h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
      <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
     </fieldset></div>
    </div>
    </body>
    </html>

以下错误,

E/Volley: [514] BasicNetwork.performRequest: Unexpected response code 401 for http://....

我搜索了如何解决这个问题,但没有找到

【问题讨论】:

  • 使用改造 2.0 对开发者来说既简单又最新。
  • 我同意上述评论。您可以使用改造 2.0,它非常有益。通过查看错误,Unauthorized: Access is denied due to invalid credentials. 看起来您缺少一些身份验证密钥。此外,如果您希望以 JSON 格式回复,请使用 JsonRequst 而不是 StringRequest
  • 感谢您的回答,我使用了 okhttp 并解决了 post 问题。

标签: android post server android-volley


【解决方案1】:

我建议您使用 Retrofit 以避免此类问题。 你会写更少的代码,但会得到很多。

这是实现 API 的两个主要使用技术的主要区别是我们的 android 应用程序。您可以看到对于一个讨论意味着对于一个网络请求和响应它们将花费多少时间的性能差异。

凌空抽射

  1. 一 (1) 次讨论:560 毫秒
  2. 七 (7) 次讨论:2202 毫秒
  3. 二十五 (25) 次讨论:4275 毫秒

改造

  1. 一 (1) 次讨论:312 毫秒
  2. 七 (7) 次讨论:889 毫秒
  3. 二十五 (25) 次讨论:1059 毫秒

原帖here

Retrofit 学习教程

我发现以下教程可以有效地学习改造的基础知识

  1. https://medium.com/@prakash_pun/retrofit-a-simple-android-tutorial-48437e4e5a23

  2. https://www.journaldev.com/13639/retrofit-android-example-tutorial

【讨论】:

  • 感谢您的回答,我使用了 okhttp 并解决了 post 问题。
【解决方案2】:
/** Your login API response is an HTML error.

     1. You should before test the API with Postman if is working properly.
     2. Then I suggest you to print on Logcat the request you are doing from you're app. Based on you're code:   **/


                 Log.d("LOGIN_REQ" , AppData().MUSTERI_KAYIT);
                     StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, new AppData().MUSTERI_KAYIT,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                 .....                    
            }

            .....

                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    String auth = "Bearer " + access_token;
                    params.put("Authorization", auth);
                    params.put("Content-Type","application/x-www-form-urlencoded");
                    ....
                    ....
                    Log.d("LOGIN_REQ",params);
                    return params;
                }

            };



/**    
3. Another think based on my little backend experience you are sending the 
        Authorization and the Content type as parameters in the request body. The 
        Authorization and the Content type are headers. So using volley you should 
        override another method:



   **/

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
      Map<String, String> params = new HashMap<String, String>();
      String auth = "Bearer " + access_token;
      params.put("Authorization", auth);
      params.put("Content-Type","application/x-www-form-urlencoded");
      return params;
    }

【讨论】:

  • 感谢您的回答,我使用了 okhttp 并解决了 post 问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2013-06-27
相关资源
最近更新 更多