【问题标题】:how to pass web api(post request) url parameters as requestbody in android using okhttp如何使用okhttp在android中将web api(发布请求)url参数作为requestbody传递
【发布时间】:2018-03-03 13:48:39
【问题描述】:

我有一个 asp.net web api url,它接受查询字符串参数,但它实际上是一个 post 请求。我正在尝试在 android 中访问相同的 url,但不确定如何传递查询参数。任何帮助将不胜感激,因为我基本上不是 Android 开发人员。

这里是sn-p:

     RequestBody formBody = new FormBody.Builder()
                    .add("email", mEmail.toLowerCase())
                    .add("password", mPassword)
                    .build();

            //2. Bind the request Object
            Request req = new Request.Builder()
                    .url(loginAPI).post(formBody)
                    .build();
            Response response = client.newCall(req).execute();

【问题讨论】:

标签: android api post okhttp3


【解决方案1】:

这是解决方案:

String loginAPI = "http://api.myapi.com/api/authentication?email="+mEmail.toLowerCase()+"&password="+mPassword;

            RequestBody reqbody = RequestBody.create(null, new byte[0]);
           Request  req = new Request.Builder()
                    .url(loginAPI)
                    .method("POST", reqbody)
                    .build();

            Response response = client.newCall(req).execute();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多