Pep*_*tal 8 java android get http okhttp
我在我的应用程序OkHttp库(http://square.github.io/okhttp/)中使用,并在一个简单的GET请求中得到此异常:
Caused by: java.io.EOFException: \n not found: size=0 content=...
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191)
...
unexpected end of stream on Connection{th.it-bedna.cz:80, proxy=DIRECT@ hostAddress=85.118.128.42 cipherSuite=none protocol=http/1.1} (recycle count=0)
Run Code Online (Sandbox Code Playgroud)
对同一地址的其他Get请求可以正常工作.如果我向Chrome输入此请求,它也可以正常工作.你知道哪里有问题吗?
谢谢你的建议.
编辑:GET的代码
public Call doGetRequest(String url, Callback callback) {
com.squareup.okhttp.Request request = new com.squareup.okhttp.Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
return call;
}
Run Code Online (Sandbox Code Playgroud)
使用:
void getData()
{
String url = "http://th.it-bedna.cz/api/v2/event/1/user?i=8";
Singleton.getInstance().doGetRequest(url, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.i("CDT", "onFailure: " + e);
}
@Override
public void onResponse(Response response) throws IOException {
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果您的服务器th.it-bedna.cz如 logcat 信息中所示,您可以尝试以下代码:
OkHttpClient client = new OkHttpClient();
// GET request
Request request = new Request.Builder()
.url("http://th.it-bedna.cz")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.e(LOG_TAG, e.toString());
}
@Override
public void onResponse(Response response) throws IOException {
Log.w(LOG_TAG, response.body().string());
Log.i(LOG_TAG, response.toString());
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5430 次 |
| 最近记录: |