【问题标题】:PHP POST request to Android Volley POST requestPHP POST 请求到 Android Volley POST 请求
【发布时间】:2021-03-11 13:23:20
【问题描述】:

我的 PHP 代码可以为我处理 API 请求。现在我需要将此代码转换为 Android 应用程序,以便可以直接从 Android 调用 API。

因此,我找到了一个 Volley 实现,但 API 仍然返回“无效请求 - 请求为空”。

我正在附加 PHP 代码。提前谢谢你

PHP:

<?php
date_default_timezone_set('Europe/Prague');
$login = "email";
$wpass = "password";

$auth = sha1($login.sha1($wpass).date('H', time()));
$command = 'ping';


$request = '{
  "request":
  {
    "user":     "'.$login.'",
    "auth":     "'.$auth.'",
    "command":  "'.$command.'"
  }
}';

$url = 'https://myapi.com/json';

// POST data
$post = 'request='.urlencode($request);

// inicializace cURL session
$ch = curl_init();

// nastavení URL a POST dat
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);

// odpověď chceme jako návratovou hodnotu curl_exec()
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

// doba, po kterou skript čeká na odpověď
curl_setopt($ch,CURLOPT_TIMEOUT,100);

// vypnutí kontrol SSL certifikátů
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);

// provedení volání
$res = curl_exec($ch);

$json = json_decode($res);

...
?>

安卓:

JSONObject postData = new JSONObject();
        try {
            String email = "email";
            String pass = "password";
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeZone(TimeZone.getTimeZone("Europe/Prague"));
            String time = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
            String auth = "encrypted ...";

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("user", email);
            jsonObject.put("auth", auth);
            jsonObject.put("command", "ping");
            String payload = jsonObject.toString();
            String query = URLEncoder.encode(payload);

            postData.put("request=", query);

        } catch (JSONException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        // Instantiate the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(root.getContext());
        String url ="https://myapi.com/json";

        // Request a string response from the provided URL.
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, postData,
                response -> {
                    System.out.println(response);
                }, error -> Toast.makeText(getContext(), error.toString(), Toast.LENGTH_SHORT).show());

        // Add the request to the RequestQueue.
        queue.add(jsonObjectRequest);

编辑:我需要将此 PHP 请求代码转换为 Android Volley 请求代码。不会使用 PHP 文件!它仅作为 PHP 中的功能解决方案的一个示例,我想在 Android Volley 中使用它

编辑 #2:来自 Android Volley 的 API 调用有效,但发送 JSON POST 数据(“请求”)可能我没有很好地实现。

【问题讨论】:

  • 我不知道您的 curl 将返回什么数据,但是您是否可以使用 $json = json_encode($res) 而不是 $json = json_decode($res) ?
  • @KenLee PHP代码没问题,但我想用Android Volley请求代替PHP请求... PHP代码不会用
  • 如果 API 仍然返回“Invalid request - request is empty”,很可能是您的 Android Volley 请求有问题,导致请求为空。请仔细检查 API 用户名/auth/querystring
  • @KenLee 我想我解决了...顺便说一句我还有一个问题...等一下,我会创建评论
  • @KenLee 我解决了 :)

标签: php android post request android-volley


【解决方案1】:

我用getBody()方法解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多