【问题标题】:Android Volley JsonObjectRequest Post prameter in KotlinKotlin 中的 Android Volley JsonObjectRequest Post 参数
【发布时间】:2018-10-25 10:06:07
【问题描述】:

我在 Java 中使用 Volley 没有任何问题。 当转换为 kotlin 时,我发现请求的发送参数有问题 我尝试使用自定义请求类从请求扩展这不能解决我的问题 我也使用带有哈希映射的 JsonObjectRequest 一些错误,参数没有随请求一起发送。 也将 JsonObjectRequest 与 JSONObject 一起使用,同样的错误仍然是 之后我使用了 Post Man API,API 方面很好,没有任何问题 使用StringRequest时也没有任何问题

我的第一个 JsonObjectRequest 代码是

val url = "http://10.0.2.2/machine_project/includeJSON/system_machine.php"
val ahOBJ = JSONObject()
ahOBJ.put("dd", 2)

Log.d("TAG","kotJson")
val queu = Volley.newRequestQueue(this)
val ahReq = JsonObjectRequest(Request.Method.POST, url, ahOBJ, Response.Listener { response ->
    val str = response.toString()
    Log.d("TAG","response: $str")
}, Response.ErrorListener {
    error ->
    Log.d("TAG","response: ${error.message}")
})
queu.add(ahReq)

第二个代码是

val jr:RequestQueue = Volley.newRequestQueue(this)
            val params = HashMap<String,String>()
            params["dd"] = "2"
            Log.d("TAGTest", "Ready to go")
            val jsObj = JsonObjectRequest(Request.Method.POST,
                urlUP,
                JSONObject(params),
                Response.Listener
                {
                    response ->
                    Log.d("TAGTest", response.toString())
                },
                Response.ErrorListener {
                    error ->
                    Log.d("TAGTest", "error: ${error.message}")
                })
            jr.add(jsObj)

所有结果都是

{"error":true,"msg":"All filed required"}

这个来自后端 API 的结果

API 是

$response = array();
if($_SERVER['REQUEST_METHOD'] == 'POST'){

    if( isset($_POST['dd'])){

        require_once ('systemMachineAPI.php');

        $result = get_sm();
        if($result != NULL){
            $response['error'] = false;
            $response['msg'] = $result;
        }else{
            $response['error'] = true;
            $response['msg'] = 'We Found Some Mistake';
        }
    }else{
        $response['error'] = true;
        $response['msg'] = 'All filed required';
    }

}else{
    $response['error'] = true;
    $response['msg'] = 'Cannot connect to server';
}

如果任何人可以解决这个问题或尝试使用 kotlin post 参数使用 volley,请帮助我

【问题讨论】:

    标签: android kotlin android-volley


    【解决方案1】:

    我知道这是一篇旧帖子,我希望你已经找到了摆脱这种情况的方法,但我会发布我的解决方案,以防其他人来到这里。

    我认为,在 php 方面,您错过了对发布的字符串进行解码。

    通过在 Kotlin 上执行此操作

    val ahOBJ = JSONObject()

    ahOBJ.put("dd", 2)

    您正在为 Volley 提供一个 json 样式的对象以发送到 php 脚本,因此在 php 端您将获得一个 json 样式的字符串来处理,而不是 POST 参数。

    在PHP端试试

    // getting the posted data and decoding it to a json object
    $postedContent = json_decode(file_get_contents("php://input"));
    
    // $postedContent should now contain your 'dd' property
    
    if (property_exists($postedContent, 'dd'))
    {
        // yes, we got our property 
        echo $postedContent->dd;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2016-08-12
      • 2014-08-25
      相关资源
      最近更新 更多