简体   繁体   English

Android Volley:无法将数据传递到服务器

[英]Android Volley: Cannot pass data to server

I am trying to create a simple registration page for my android app. 我正在尝试为我的Android应用程序创建一个简单的注册页面。 I am passing parameters with URL String and Storing those parameters in Database. 我使用URL字符串传递参数并将这些参数存储在数据库中。 Whenever i try to use string directly in browser the data is added to database without any error. 每当我尝试直接在浏览器中使用字符串时,数据都会毫无错误地添加到数据库中。 However, When i try to pass data from Android Volley i am getting HTTP Error 409. 但是,当我尝试从Android Volley传递数据时,出现HTTP错误409。

I have checked everything and still confused why this error is appearing only when i try to run url string from android app. 我检查了所有内容,仍然感到困惑,为什么只有当我尝试从android应用程序运行url字符串时才会出现此错误。

URL String:- 网址字符串:

http://myurl.com/api/register.php?name=ahmed&email=obaid.ahmed@gmail.com&password=12345&membertype=Artist&joindate=24-Feb-2019

Java Code:- Java代码:

       JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, urlFinal, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // display response
                    Log.d("Response", response.toString());
                    uploadImageToServer();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                    hideDialog();
                }
            }
    );

First you need to understand about 409, 首先,您需要了解409,

Explain here :- https://httpstatuses.com/409 在这里解释:-https: //httpstatuses.com/409

And try to solve this on Server side where this Conflict issue occur. 并尝试在发生此冲突问题的服务器端解决此问题。

And I recommend you to use URL Encoding before sending things in url, Which is perform by : Either by, 而且我建议您在通过url发送内容之前使用URL编码,该方法由以下方式执行:

  String query = URLEncoder.encode("apples oranges", "utf-8");
  String url = "http://stackoverflow.com/search?q=" + query;

or By

  String uri = Uri.parse("http://...")
            .buildUpon()
            .appendQueryParameter("key", "val")
            .build().toString();

Which make your URL more compatible. 这使您的URL更兼容。

Or for More Better ways, There is an Library which i personally recommend to any android developer who love Light weight Volley for Api Integrations. 或以更好的方式,有一个我个人推荐给喜欢Light Volley for Api Integrations的Android开发人员使用的库。

https://github.com/Lib-Jamun/Volley

dependencies {
   compile 'tk.jamun:volley:0.0.4'
}

Cool Coding. 酷编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM