【发布时间】:2018-08-10 05:33:30
【问题描述】:
您好,我在 Android 中使用 Okhttp3 库为 cURL 调用创建了一个 GET 调用,如下所示:
curl -X GET "http://1.1.1.1:8080/api/v1/login" -H "accept: application/json" -H "Authorization: Bearer 123456"
这样在Android中使用OkHttp3库
final String url = BuildConfig.BASE_AUTH_URL + "/login" + "/" + token;
Request request = new Request.Builder()
.url(url)
.addHeader("Content-type", "application/json")
.get()
.build();
...
但是在我在 logcat 中打印的日志中,请求失败(500 内部服务器错误),因为 url 在 "Bearer" 后面有一个 "%20" 作为 "空格"
Request{method=GET, url=http://1.1.1.1:8080/login/Bearer%20123456
错误是<html><head><title>Error</title></head><body>Internal Server Error</body></html>
org.json.JSONException: Value <html><head><title>Error< of type java.lang.String cannot be converted to JSONObject
我尝试使用 token.replace("%20", " ") 方法或 URLEncoder.encode(token, "UTF-8") 等方法,但没有一个有效。
有人可以帮我以正确的方式编码,但网址中没有“%20”字符吗?
谢谢
【问题讨论】:
标签: android api replace okhttp