BaseOkHttpV3 icon indicating copy to clipboard operation
BaseOkHttpV3 copied to clipboard

对于post 请求 Query Params方式传参?怎么传?

Open honganxue opened this issue 4 years ago • 5 comments

honganxue avatar Sep 29 '20 02:09 honganxue

您好,请使用 HttpUrl 创建,在传入 url 参数时使用 httpUrl.toString():

HttpUrl httpUrl= new HttpUrl.Builder()
    .scheme("https")
    .host("www.example.com")
    .addQueryParameter("key", "value")
    .build();

请求时:

HttpRequest.build(context, httpUrl.toString())
        .setResponseListener(new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                ...
            }
        })
        .doPost();

kongzue avatar Sep 29 '20 04:09 kongzue

host 传入ip地址报错

honganxue avatar Sep 29 '20 04:09 honganxue

请说明是什么错误

kongzue avatar Sep 29 '20 04:09 kongzue

image 2020-09-29 12:47:16.125 11246-11246/com.example.transferstation.activity E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.transferstation.activity, PID: 11246 java.lang.IllegalArgumentException: unexpected host: 120.24.62.145:8153/admin/login/token at okhttp3.HttpUrl$Builder.host(HttpUrl.java:1035) at com.example.transferstation.activity.LoginActivity.login(LoginActivity.java:89) at com.example.transferstation.activity.LoginActivity$2.onClick(LoginActivity.java:77) at android.view.View.performClick(View.java:6597) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

honganxue avatar Sep 29 '20 04:09 honganxue

您好,您的创建方式有误。 请使用如下方式创建:

HttpUrl httpUrl = new HttpUrl.Builder()
        .scheme("http")
        .host("120.24.62.145")
        .port(8153)
        .addPathSegment("admin")
        .addPathSegment("login")
        .addPathSegment("token")
        .addQueryParameter("password","123456")
        .addQueryParameter("username","test")
        .build();

kongzue avatar Sep 29 '20 04:09 kongzue