【问题标题】:Got Invalid Content Type while implementing message 91 API?实现消息 91 API 时内容类型无效?
【发布时间】:2019-06-06 07:15:44
【问题描述】:

我遇到了一个问题,当我点击 Message 91 API 时,我得到了无效的内容类型

我使用 AndroidFastNetworking 库来实现 API,它在 Postman 中完美运行,但面临错误“内容类型无效”

private void sendMsg(String no, String msg) {
        //send msg API call
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("sender", "NAINIS");
            jsonObject.put("route", "4");
            jsonObject.put("country", "91");
            JSONArray jsonArray = new JSONArray();
            JSONObject joSMS = new JSONObject();
            jsonArray.put(joSMS);
            jsonObject.put("sms", jsonArray);
            joSMS.put("message", msg);
            JSONArray jaNumber = new JSONArray();
            joSMS.put("to", jaNumber);
            jaNumber.put(no);

            Log.i("jsonObject", "" + jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        AndroidNetworking.post("https://api.msg91.com/api/v2/sendsms")
                .addHeaders("authkey", "27XXXXXXXXXXXXXa")
                .addHeaders("Content-Type", "application/json")
                .addQueryParameter("country","91XXXXXXXX8")
                .addJSONObjectBody(jsonObject) // posting json
                .setTag("test")
                .setPriority(Priority.MEDIUM)
                .build()
                .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("response", "" + response);
                    }

                    @Override
                    public void onError(ANError error) {
                        Log.d("error", "" + error.getErrorBody());
                    }
                });
    }

这里出现了类似的错误

{"type":"error","message":"Invalid content type.Please send data in formdata,application\/xml,application\/json format","code":""}

【问题讨论】:

    标签: android sms-gateway android-webservice


    【解决方案1】:

    此链接可能会有所帮助

    https://github.com/square/okhttp/issues/2099

    我遇到了类似的问题,其中 okhttp 客户端将内容类型发送为“application/json”,而字符集发送为“utf-8”,而 msg91 服务器无法处理此问题。

    我从它工作的 jsonstring 字节创建了有效负载。

    【讨论】:

    猜你喜欢
    • 2015-10-24
    • 2018-04-12
    • 1970-01-01
    • 2017-11-08
    • 2016-08-21
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多