我在我的代码中有基于HTML的查询,并且一个特定类型似乎IOExceptions在收到来自服务器的505响应时产生.我和其他似乎有类似问题的人一起查看了505的回复.显然505表示HTTP版本不匹配,但是当我将相同的查询URL复制到任何浏览器(尝试过firefox,seamonkey和Opera)时,似乎没有问题.我读过的其中一篇帖子表明浏览器可能会自动处理版本不匹配问题.
我试图通过使用Opera附带的漂亮的开发人员工具深入挖掘,看起来版本中没有不匹配(我相信Java使用HTTP 1.1)并且200 OK收到了很好的响应.当同一查询通过我的Java代码时,为什么会遇到问题?
private InputStream openURL(String urlName) throws IOException{
URL url = new URL(urlName);
URLConnection urlConnection = url.openConnection();
return urlConnection.getInputStream();
}
Run Code Online (Sandbox Code Playgroud)
示例链接: http://www.uniprot.org/uniprot/?query=mnemonic%3aNUGM_HUMAN&format=tab&columns=id,entry%20name,reviewed,organism,length
Java的I/O类java.io.Reader,java.io.Writer,java.io.InputStream,java.io.OutpuStream和他们的不同子类中都有一个close()可抛出的方法IOException.
对于处理此类例外的正确方法是否有任何共识?
我经常看到建议只是默默地忽略它们,但这感觉不对,至少在开放写入资源的情况下,关闭文件时出现问题可能意味着无法写入/发送未刷新的数据.
另一方面,在阅读资源时,我完全不清楚为什么close()会扔掉它以及如何处理它.
那么有任何标准建议吗?
一个相关的问题是,close是否会抛出IOException?,但更多的是它的实现确实不丢,不是关于如何处理异常.
这是我的代码:
private boolean writeToSD(Bitmap bm, String url) {
if (canIWriteOnSD()) {
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, "MoveInBlue/");
try {
url = urlCleaner(url);
if (!dest.exists()) {
dest.mkdir();
}
File file = new File(dest, url + ".png");
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
// Do nothing
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
问题解决了:
urlCleaner现在返回url.substring(url.lastIndexOf('?')+ 1),一切都按预期工作.
实际上是例外file.createNewFile();,我不知道为什么.
非常感谢你.
(urlCleaner只是从URL中删除http:// ...并保留php标记)
这是LogCat: …
最终更新 这是我们的固件.令人尴尬到一定程度,但我很高兴我们可以前进,我可以把学习Java关闭一天.我的答案如下.
更新 所以我或多或少地放弃了这一点.我认为这是一个可以归结为API的错误,但我没有时间,资源和技能来深入了解它.我认为存在一些Windows只是中指的硬件.我已经下载了Eclipse,切换到Java并将尝试查看是否有效.如果没有,你会看到我回到这里.但是,我绝对喜欢解决这个问题,所以如果有人有时间或倾向深入研究这个问题,我很乐意看到你提出的问题.显然我会不时回来看看.请确保你在评论中'@'我,所以我收到了提醒.
原始邮政
我知道还有其他一些人在处理这个问题,但我希望有人可以帮助我.我正在尝试连接到COM端口,但是当我尝试使用该serialport.Open()命令时,我收到了IO异常:
System.IO.IOException: The parameter is incorrect.
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.InternalResources.WinIOError()
at System.IO.Ports.SerialStream.InitializeDCB(Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Boolean discardNull)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
at System.IO.Ports.SerialPort.Open()
at *programtitlehere.cs*:line 90
Run Code Online (Sandbox Code Playgroud)
我使用Stellaris LM4F232来模拟COM端口.我可以使用Termite(终端程序)打开,访问并获得良好的结果,但每当我尝试使用Visual Studio时,它甚至都无法连接,我收到此错误.现在我甚至不知道这个错误意味着什么,尽管我试图在别处读书,但我仍然感到迷茫.
任何人都可以向我解释这里发生了什么,也许我可以开始尝试解决这个问题吗?我可以包含更多代码,但说实话,那里没有太多代码; 串口设备的所有属性都是正常的,它只发生在这个设备上(我可以使用MSP430没有相同细节的问题).
对于想要看到它的人,我的代码如下所示(注意这只是一个'沙盒',而不是实际的程序,但症状是相同的):
try
{
serialPort1.PortName = "COM5";
serialPort1.Open();
if (serialPort1.IsOpen == true)
{
textBox1.Text = …Run Code Online (Sandbox Code Playgroud) 我第一次得到
引起:java.io.IOException:HTTP/1.1标头解析器未收到字节
(我的应用程序到目前为止似乎可以运行......)
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest
.newBuilder()
.GET()
.uri(URI.create("..."))
.header("Content-Type", "application/json")
.build();
System.out.print(httpRequest.toString()); // dbg - it's ok
HttpResponse<String> response = null;
try {
response = httpClient.send(httpRequest,
HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
if (response != null && response.statusCode() == 200) {
JSONObject jsonObject = new JSONObject(response.body());
JSONArray jsonArray = jsonObject.getJSONArray("data");
ObjectMapper objectMapper = ObjectMapperCreator.getNewObjectMapper();
try {
if (jsonArray.length() > 0) {
correctlyCaught = true;
return objectMapper.readValue(jsonArray.get(0).toString(), GeocodingResponse.class);
}
} catch …Run Code Online (Sandbox Code Playgroud) 最终编辑:我找到了问题的解决方案(在问题的底部).
我有一个让你感到悲伤的Nunit问题.编辑:实际上它看起来更像是一个SQLite问题,但我还不是100%肯定.
我的TestFixture有一个设置,可以生成一个随机文件名,在我的每个测试中都用作SQLite数据库.
[Setup]
public void Setup()
{
// "filename" is a private field in my TestFixture class
filename = ...; // generate random filename
}
Run Code Online (Sandbox Code Playgroud)
我的每个测试都在访问数据库的每个方法中使用此构造:
[Test]
public void TestMethod()
{
using (var connection = Connect())
{
// do database activity using connection
// I've tried including this line but it doesn't help
// and is strictly unnecessary:
connection.Close();
}
}
private DbConnection Connect()
{
var connection = DbProviderFactories.GetFactory("System.Data.SQLite").CreateConnection();
connection.ConnectionString = "Data Source=" + filename;
connection.Open(); …Run Code Online (Sandbox Code Playgroud) 昨天晚上,我离开了办公室,运行了一个由我编写的Java程序.它应该使用JDBC连接将大量记录插入到我们的公司数据库(Oracle)中.今天早上当我回来工作时,我看到了这个错误(由try-catch捕获):
java.sql.SQLRecoverableException: I/O Exception: Connection reset
Run Code Online (Sandbox Code Playgroud)
在得到这个问题之前,该程序几乎写了所有记录,但如果它发生得很早(我晚上离开办公室几分钟后)怎么办?我无法理解发生了什么,我联系了我的数据库管理员,他说数据库没有特别的问题.
关于发生了什么以及我可以做些什么来避免它的任何想法?
我目前正在构建一个使用新的Google Cloud Messaging系统的应用程序.我在实时环境中设置了一个HTTP客户端服务器,允许我通过我的移动设备进行测试,但是当我尝试向GCM注册时,我不断收到一条IOException,指出gcm.register()接收到未知来源阻止我获取注册ID我需要允许我的服务器向我的设备发送消息.
我已经多次检查过我的发件人ID是否错误,我已检查过我的清单,看看我是否也有错误,我似乎无法找到问题.环顾四周也没有为我的问题提供任何答案.
这是我的日志:
07-18 11:34:25.487: W/System.err(3093): java.io.IOException: MAIN_THREAD
07-18 11:34:25.497: W/System.err(3093): at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
07-18 11:34:25.497: W/System.err(3093): at koodoo.pushtest.MainActivity.onCreate(MainActivity.java:94)
07-18 11:34:25.497: W/System.err(3093): at android.app.Activity.performCreate(Activity.java:4492)
07-18 11:34:25.497: W/System.err(3093): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 11:34:25.497: W/System.err(3093): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 11:34:25.497: W/System.err(3093): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 11:34:25.497: W/System.err(3093): at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 11:34:25.497: W/System.err(3093): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 11:34:25.497: W/System.err(3093): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 11:34:25.497: W/System.err(3093): at android.os.Looper.loop(Looper.java:137)
07-18 11:34:25.497: W/System.err(3093): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 11:34:25.497: W/System.err(3093): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:34:25.497: W/System.err(3093): at java.lang.reflect.Method.invoke(Method.java:511)
07-18 …Run Code Online (Sandbox Code Playgroud) android ioexception push-notification google-cloud-messaging
在试图回答这个问题时,我惊讶地发现,当该文件已经存在时尝试创建一个新文件不会抛出一个唯一的异常类型,它只会抛出一个泛型IOException.
因此,我想知道如何确定它IOException是现有文件的结果还是其他一些IO错误.
该异常具有HResult,但此属性受到保护,因此对我无法使用.
我能看到的另一种方式是模式匹配感觉糟糕的消息字符串.
例:
try
{
using (var stream = new FileStream("C:\\Test.txt", FileMode.CreateNew))
using (var writer = new StreamWriter(stream))
{
//write file
}
}
catch (IOException e)
{
//how do I know this is because a file exists?
}
Run Code Online (Sandbox Code Playgroud) 在我的OS X Yosemite中,我正在尝试在终端中启动域:
sh asadmin start-domain mythMobile
Run Code Online (Sandbox Code Playgroud)
我收到错误:
JVM failed to start: java.io.IOException: Cannot run program "/usr/libexec/StartupItemContext" (in directory "/Applications/glassfish4/glassfish/domains/mythMobile/config"): error=2, No such file or directory
Command start-domain failed.
Run Code Online (Sandbox Code Playgroud)
我在我的Mac上安装了Beta Yosemite后出现了这个问题.在我有小牛队之前,一切正常.
有人遇到过这样的问题吗?