我有一个可以在手机(Huawei Mate 10 Pro)上完美运行的android应用,但是当我尝试在平板电脑(三星Galaxy Tab S2)上运行该应用时,当我尝试访问该应用时,我会收到java.io.FileNotFoundException输入流。
(在“ InputStream流= connection.getInputStream();”处出现异常)
三星和其他设备之间在http通信方面是否有所不同?如何编写可在每台设备上使用的代码?
代码如下:
public String download_organisations(String url){
String jsonString = "";
try {
if(!url.startsWith("http://")){
url = "http://" + url;
}
if(url.endsWith("/")){
url = url.substring(0, url.lastIndexOf("/"));
}
url = url + ManagerSystemStatic.URL_SERVICE_WEB_ORGANISATION_MANAGER;
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.setConnectTimeout(10000);
InputStream stream = connection.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] inputStreamByteArray = buffer.toByteArray();
byte[] base64 = Base64.decode(inputStreamByteArray, 0);
byte[] decrypted = CipherUtils.decrypt(base64);
jsonString = new String(decrypted);
stream.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.d("OrganisationManager", "Download succeded with response: " + connection.getResponseCode());
} else {
Log.d("OrganisationManager", "Download failed with response: " + connection.getResponseCode());
}
} catch (MalformedURLException e) {
Log.e("OrganisationManager", e.toString());
return DOWNLOAD_FAILED;
} catch (IOException e) {
Log.e("OrganisationManager", e.toString());
e.printStackTrace();
return DOWNLOAD_FAILED;
} catch (Exception e) {
Log.e("OrganisationManager", e.toString());
return DOWNLOAD_FAILED;
}
return jsonString;
}
这是StackTrace(替换为我的网址):
E/OrganisationManager: java.io.FileNotFoundException: http://www.myurlhere.com
W/System.err: java.io.FileNotFoundException: http://www.myurlhere.com
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:254)
W/System.err: at com.organisationmanager.ble.common.WebPortalCommunicationHelper.download_organisations(WebPortalCommunicationHelper.java:210)
at com.organisationmanager.ble.ScanningActivity$UpdateOrganisations.doInBackground(ScanningActivity.java:414)
at com.organisationmanager.ble.ScanningActivity$UpdateOrganisations.doInBackground(ScanningActivity.java:403)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
D/InputTransport: Input channel constructed: fd=82
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err: at java.lang.Thread.run(Thread.java:762)
当它开始http通信时,我也在调试器控制台中得到了它:
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
相关吗?
答案 0 :(得分:0)
经过反复试验,我仍然不知道问题出在什么地方,但是我尝试使用OkHttp类而不是HttpUrlConnection,突然一切正常。我不知道为什么,但是由于我现在有一个工作代码,因此不再需要此问题的帮助。我希望这种解决方案(完全改变班级)将来可以对某人有所帮助。有关我使用的课程的更多信息,请参见:http://square.github.io/okhttp/