我正在尝试从我的服务器下载文件。如果我通过浏览器发出这个请求,我会得到所需的文件,但是当我使用 volley 时,我首先得到 com.android.volley.TimeoutError 在我使用 setRetryPolicy 之后我得到 p>
BasicNetwork.performRequest: Unexpected response code 200
这是我进行下载的类(class):
public class DefexConnection implements Response.Listener<byte[]>, Response.ErrorListener {
private Context ctx;
private DefexDownload download;
public DefexConnection(Context ctx){
this.ctx = ctx;
}
public void download(String link, final HashMap<String, String> params){
DefexDownload req = new DefexDownload(Request.Method.GET, link, DefexConnection.this, DefexConnection.this, params);
req.setRetryPolicy(new DefaultRetryPolicy( 500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue queue = Volley.newRequestQueue(this.ctx, new HurlStack(null, getSocketFactory()));
queue.add(req);
}
private SSLSocketFactory getSocketFactory() {
CertificateFactory cf = null;
try {
cf = CertificateFactory.getInstance("X.509");
InputStream caInput = ctx.getResources().openRawResource(R.raw.comodoroca);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
Log.e("CERT", "ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
Log.d("CipherUsed", session.getCipherSuite());
return hostname.compareTo("www.defexsecurity.com")==0;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
SSLContext context = null;
context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
SSLSocketFactory sf = context.getSocketFactory();
return sf;
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
@Override
public void onResponse(byte[] response) {
HashMap<String, Object> map = new HashMap<String, Object>();
int count;
if (response!=null) {
String content = download.responseHeaders.get("Content-Disposition").toString();
StringTokenizer st = new StringTokenizer(content, "=");
String[] arrTag = st.toArray();
String filename = arrTag[1];
filename = filename.replace(":", ".");
Log.d("DEBUG::RESUME FILE NAME", filename);
try{
long lenghtOfFile = response.length;
//covert reponse to input stream
InputStream input = new ByteArrayInputStream(response);
File path = Environment.getExternalStorageDirectory();
File file = new File(path, filename);
map.put("resume_path", file.toString());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
Log.e("DEFEX","file saved to "+file.getAbsolutePath());
}catch(IOException e){
e.printStackTrace();
}
}
}
@Override
public void onErrorResponse(VolleyError error) {
Log.e("DEFEX", "UNABLE TO DOWNLOAD FILE. ERROR:: "+error.toString());
}
这是我的 DefexDownload 类(它扩展了 Request):
class DefexDownload extends Request<byte[]>{
private final Response.Listener<byte[]> mListener;
private Map<String, String> mParams;
public Map<String, String> responseHeaders ;
DefexDownload(int post, String mUrl, Response.Listener<byte[]> listener, Response.ErrorListener errorListener, HashMap<String, String> params){
super(post, mUrl, errorListener);
setShouldCache(false);
mListener = listener;
mParams=params;
}
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
return mParams;
}
@Override
protected void deliverResponse(byte[] response) {
mListener.onResponse(response);
}
@Override
protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
//Initialise local responseHeaders map with response headers received
responseHeaders = response.headers;
//Pass the response data here
return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));
}
}
我不知道这里可能出了什么问题,我们将不胜感激任何帮助
编辑:关于我在 SO 上找到的所有其他答案,问题似乎出在代理或设备模拟器上,但我在未使用任何代理的真实设备上运行我的应用
最佳答案
根据这个issue在 Github 上。
So
HurlStack
is more or less just a thin wrapper aroundHttpUrlConnection
. TheEOFException
is presumably coming from there when trying to make the connection. I don't think there is much that Volley can do about this, short of applying a safe workaround if there's a known platform bug.The stack traces of the cause chain would be good to look at, since the exception is being thrown by some platform class under the covers. Otherwise, I suspect you would reproduce the same issue if you just used
HttpUrlConnection
directly, which means there's unfortunately not much Volley can do.Searching for the exception indicates a number of potential issues such as Android HttpUrlConnection Url doesn't work on emulator indicating networking issues on the machine in question which explained why it was emulator only.
问题预期:
- 模拟器连接配置不当
- 主机 PC 防病毒或防火墙。
解决方案
- 使用真实设备尝试您的代码。
- 在与主机 PC 配置网络并停止防病毒或/和防火墙后尝试使用模拟器。
关于java - 使用 Volley 下载时出现错误 : Unexpected response code 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51812923/