java - 如何从 Android 应用程序向 WHMCS 安装发送正确的发布请求?

标签 java android http request whmcs

我正在尝试通过我的 Android 应用程序向我的 WHMCS 安装发送 AddClient post 请求,但它不断返回“result=error;message=您没有输入您的名字”。

我成功验证了它,但它似乎没有发布我发送的 jsonObject。

这里是 WHMCS api 文档的链接:https://developers.whmcs.com/api-reference/addclient/

public void createWHMCSUser(final String emailID, final String random, final String uid) {

    final String AccessUserKey = "abc123";
    final String AccessKey = "abc123";
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("http://whmcs.installation.com/includes/api.php?action=AddClient&username=abc123&password=abc123&accesskey=abc123");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Accept","application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);

                JSONObject jsonParam = new JSONObject();
                jsonParam.put("action", "AddClient");
                jsonParam.put("identifier", AccessUserKey);
                jsonParam.put("secret", AccessKey);
                jsonParam.put("firstname", "User");
                jsonParam.put("lastname", "Name");
                jsonParam.put("email", emailID);
                jsonParam.put("address1", "na");
                jsonParam.put("city", "na");
                jsonParam.put("state", "na");
                jsonParam.put("postcode", "00000");
                jsonParam.put("country", "US");
                jsonParam.put("phonenumber", "0000000000");
                jsonParam.put("password2", random);
                jsonParam.put("repsonsetype", "json");

                Log.i("JSON", jsonParam.toString());
                DataOutputStream os = new DataOutputStream(conn.getOutputStream());
                os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
                //os.writeBytes(jsonParam.toString());

                os.flush();
                os.close();

                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG", conn.getResponseMessage());



                conn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

    thread.start();

}

它应该返回“result=success”。

但实际上返回“result=error;message=您没有输入您的名字”。

最佳答案

看来您的 API 在此示例中不起作用。

尝试Postman如果 json 在那里不起作用,则问题出在您的 api 中。

关于java - 如何从 Android 应用程序向 WHMCS 安装发送正确的发布请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466073/

相关文章:

http - Cache-Control : max-age=0 and no-cache?有什么区别

android - Android DefaultHttpClient 中超时值的默认连接

java - 提供单个@Bean,可用于在多个接口(interface)中注入(inject)

Web服务数据更改时的Android通知

android - Google Play 服务投诉将过时

java - Android、Butterknife、BindView、最终

javascript - JS中的哈希密码

Java Socket 类转换异常

java - 如何处理 swing 中图像显示的错误文件选择

java - 使用 if 语句检查 While 循环