我有一个像这样使用凌空的简单应用程序:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("VolleyController", "Received volley response: " + response);
callback.onCallback(OperationOutcome.SUCCESS, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// This is null.
NetworkResponse response = error.networkResponse;
Log.e("VolleyManager", "Received error response from volley: ", error );
callback.onCallback(OperationOutcome.FAILURE, null);
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
这是我得到的例外:
Received error response from volley: com.android.volley.NoConnectionError: java.io.EOFException
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:98)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:202)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:119)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:798)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
我在网上找到了一些其他的答案,但他们并没有解决。请求未到达服务器。
非常感谢。
修改
我在清单上都有权限:
<uses-permission android:name="android.permission.INTERNET" />
并且互联网在设备上正常运行。
答案 0 :(得分:0)
您的设备上是否启用了互联网? 您是否在清单中设置了INTERNET权限?
检查您的网址是否正确。
答案 1 :(得分:0)
问题是由于我发送的请求是空的。我认为Volley会将其自身转换为正确的JSON表示({}
)。相反,我认为请求只是一个null
,这导致了异常。为解决此问题,我更改了StringRequest
中的JSONObjectRequest
。这有一个构造函数,它将JSONObject
作为输入:
// This will instantiate an empty JSON request with the correct format.
new JSONObject("{}");
我仍然不明白为什么Volley不会独自完成,我只是出于好奇而理解。无论如何,在此之后,我决定改用Retrofit,它的设置更高效,更简单。
谢谢大家的帮助!