当我使用OKHttp请求链接时,它崩溃了,因为它不是正确的链接,我该如何捕获崩溃,并且我不确定将来是否会出现这些错误的链接。我需要抓捕他以避免程序崩溃
本文是通过翻译工具生成的,如果您听不懂,请告诉我。
try {
val request = Request.Builder()
.url("http://pic1.nipic.com|/2008-12-30/200812308231244_2.jpg ")
.build()
val client = OkHttpClient
.Builder()
.build()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
val s = response.body().toString()
println(s)
}
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
})
} catch (e: Exception) {
}
我希望我能捕获它并避免崩溃。
java.lang.RuntimeException: java.net.URISyntaxException: Illegal character in authority at index 21: http://pic1.nipic.com|/
at okhttp3.HttpUrl.uri(HttpUrl.java:386)
at okhttp3.internal.connection.RouteSelector.resetNextProxy(RouteSelector.java:129)
at okhttp3.internal.connection.RouteSelector.<init>(RouteSelector.java:63)
at okhttp3.internal.connection.StreamAllocation.<init>(StreamAllocation.java:101)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.net.URISyntaxException: Illegal character in authority at index 21: http://pic1.nipic.com|/
at libcore.net.UriCodec.validate(UriCodec.java:63)
at java.net.URI.parseURI(URI.java:395)
at java.net.URI.<init>(URI.java:205)
at okhttp3.HttpUrl.uri(HttpUrl.java:379)
at okhttp3.internal.connection.RouteSelector.resetNextProxy(RouteSelector.java:129)
at okhttp3.internal.connection.RouteSelector.<init>(RouteSelector.java:63)
at okhttp3.internal.connection.StreamAllocation.<init>(StreamAllocation.java:101)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
好吧,这是因为您在url中传递了一个特殊字符,从而使它被破坏了。为避免崩溃,您只需在执行代码之前从URL中删除特殊字符即可:
public class XpsPrinterHelper
{
public bool Print(byte[] documentImage, string printerName, string tempXpsFilename = "report.xps")
{
var success = false;
var thread = new Thread(() =>
{
using (var printerQueue = GetPrintQueueFromPrinterName(printerName))
{
try
{
//save file temporary
var filename = Path.Combine(Directory.GetCurrentDirectory(), tempXpsFilename);
File.WriteAllBytes(filename, documentImage);
using (var printSystemJobInfo = printerQueue.AddJob())
{
printSystemJobInfo.JobStream.Write(documentImage, 0, documentImage.Length);
}
success = true;
}
catch (PrintJobException)
{
printerQueue.Dispose();
}
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join(); //Wait until finished
return success;
}
private static PrintQueue GetPrintQueueFromPrinterName(string printerName)
{
if (printerName.StartsWith(@"\\")) //If it is a shared printer
{
var serverPath = printerName.Substring(0, printerName.LastIndexOf('\\'));
var sharedPrinterName = printerName.Replace(serverPath, "");
var printServer = new PrintServer(serverPath.TrimEnd('\\'));
return printServer.GetPrintQueue(sharedPrinterName.Trim('\\'));
}
var localPrintServer = new LocalPrintServer();
return localPrintServer.GetPrintQueue(printerName);
}
}
答案 1 :(得分:0)
基本上,您可以使用try catch
块
private fun testing() {
val request = Request.Builder()
try {
val httpUrl = HttpUrl.parse("http://pic1.nipic.com|/2008-12-30/200812308231244_2.jpg")?.uri()
httpUrl?.let {
request.url(it.toURL())
}
}catch (e: Exception) {
Log.d("Testing", e.toString())
return
}
val client = OkHttpClient.Builder().build()
client.newCall(request.build()).enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
Log.d("Testing", e.toString())
}
override fun onResponse(call: Call, response: Response) {
Log.d("Testing", response.body().toString())
}
})
}
}
请参阅下面的链接以获得更多讨论 https://github.com/square/okhttp/issues/543