【问题标题】:Android Volley: Cannot pass data to serverAndroid Volley:无法将数据传递到服务器
【发布时间】:2019-02-23 20:01:22
【问题描述】:

我正在尝试为我的 Android 应用创建一个简单的注册页面。我正在使用 URL 字符串传递参数并将这些参数存储在数据库中。每当我尝试直接在浏览器中使用字符串时,数据都会毫无错误地添加到数据库中。但是,当我尝试从 Android Volley 传递数据时,我收到 HTTP 错误 409。

我已经检查了所有内容,但仍然困惑为什么只有当我尝试从 android 应用程序运行 url 字符串时才会出现此错误。

网址字符串:-

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

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();
                }
            }
    );

【问题讨论】:

  • 不确定这是否与错误有关,但不应该是POST 请求吗?
  • @paulajcm 是的,我尝试过 POST 请求,但我的服务器没有通过 POST 获取任何变量或数据。

标签: java php android android-volley


【解决方案1】:

首先你需要了解一下409,

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

并尝试在发生此冲突问题的服务器端解决此问题。

而且我建议您在使用 url 发送内容之前使用 URL 编码, 由以下人员执行: 无论是通过,

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

或由

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

这使您的 URL 更兼容。

或者为了更好的方法,我个人向任何喜欢轻量​​级 Volley for Api 集成的 Android 开发人员推荐一个库。

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

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

酷编码。

【讨论】:

  • 非常感谢您的回答。实际上,我的系统或 Internet 连接或 Android Studio 存在问题。我重新启动了所有这些并且它起作用了。 :)
猜你喜欢
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 2017-07-01
相关资源
最近更新 更多