相关疑难解决方法(0)

Java:已检查vs未经检查的异常说明

我已经在StackOverFlow上阅读了有关已检查和未经检查的异常的多个帖子.老实说,我还是不太确定如何正确使用它们.

Joshua Bloch在" Effective Java "中说过

对可恢复条件使用已检查的异常,对编程错误使用运行时异常(第2版中的第58项)

让我们看看我是否正确理解这一点.

以下是我对已检查异常的理解:

try{
    String userInput = //read in user input
    Long id = Long.parseLong(userInput);
}catch(NumberFormatException e){
    id = 0; //recover the situation by setting the id to 0
}
Run Code Online (Sandbox Code Playgroud)

1.以上是否考虑了检查异常?

2. RuntimeException是未经检查的异常吗?

以下是我对未经检查的异常的理解:

try{
    File file = new File("my/file/path");
    FileInputStream fis = new FileInputStream(file);   
}catch(FileNotFoundException e){

//3. What should I do here?
    //Should I "throw new FileNotFoundException("File not found");"?
    //Should I log?
    //Or should I System.exit(0);?
}
Run Code Online (Sandbox Code Playgroud)

4.现在,上述代码也不能成为检查异常吗?我可以尝试恢复这样的情况吗?我可以吗?(注意:我的第3个问题在catch上面)

try{
    String …
Run Code Online (Sandbox Code Playgroud)

java exception runtimeexception checked-exceptions unchecked-exception

678
推荐指数
11
解决办法
31万
查看次数

在Java中哪里可以获得"UTF-8"字符串文字?

我试图在这段代码中使用常量而不是字符串文字:

new InputStreamReader(new FileInputStream(file), "UTF-8")
Run Code Online (Sandbox Code Playgroud)

"UTF-8"经常出现在代码中,而且更好地引用一些static final变量.你知道我在JDK哪里可以找到这样一个变量吗?

顺便说一句,第二个想法,这样的常量是糟糕的设计:公共静态文字...不是数据复制的解决方案

java

450
推荐指数
9
解决办法
26万
查看次数

解码android中的HTML实体

我需要解码HTML实体,例如来自ö 到ö,和& 至 &.

URLEncoder.decode(str)不做这项工作(从%表示法转换).TextUtils有一个HTMLencode,但不是HTMLdecode.

是否有解码HTML实体的功能?

html android decode

63
推荐指数
3
解决办法
5万
查看次数

如何解码Android应用程序中的URL

可能重复:
如何在Java中进行URL解码?

我正在尝试编写一个Android应用程序,它应该检索CDN URL的内容,这些内容始终是URLEncoded.为了举例,我使用的是youtube网址,因为它们的格式相同.

    public static String getText(String url) throws Exception {
    URL website = new URL(url);
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                                connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        response.append(inputLine);

    in.close();

    return response.toString();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
    String page = getText("http://www.youtube.com/watch?v=QH2-TGUlwu4");
    String[] temper = page.split("flashvars=\"");
    page = temper[1];
    temper = page.split("\"     allowscriptaccess=\"always\"");
    page …
Run Code Online (Sandbox Code Playgroud)

java url android

20
推荐指数
2
解决办法
3万
查看次数

Java 6中的URL解码

我看到它java.net.URLDecoder.decode(String)在6 中被弃用了.

我有以下字符串:

String url ="http://172.20.4.60/jsfweb/cat/%D7%9C%D7%97%D7%9E%D7%99%D7%9D_%D7%A8%D7%92%D7%99%D7%9C%D7%99%D7%9"
Run Code Online (Sandbox Code Playgroud)

我应该如何在Java 6中解码它?

java

19
推荐指数
4
解决办法
4万
查看次数

如何在Android上的WebView中读取Blob中的数据?

我有一个服务器在浏览器上创建一个对象blob,我想在Android应用程序的WebView中下载它.我尝试将请求重定向到浏览器实例以及使用下载管理器执行此操作,但它们似乎都不起作用(即使我在Chrome中打开相同的页面,下载操作也在那里工作).

我尝试了以下代码,它抛出错误:

Android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.intent.action.VIEW dat = blob:https%3A // 111.111.111.111%3A8080/40b63131-63b1-4fa4-9451-c6297bbd111a"

编辑

Android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.intent.action.VIEW dat = blob:http://digitalinsensu.com/0f0d6127-a0f1-44c3-af85-72f648258d6d


码:

mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}
});
Run Code Online (Sandbox Code Playgroud)

这会抛出java.lang.IllegalArgumentException:只能下载HTTP/HTTPS URIs错误:

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,String contentDisposition, String mimetype,long contentLength) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);

    }
});
Run Code Online (Sandbox Code Playgroud)

我该如何下载blob?任何帮助,将不胜感激.

android blob android-webview

6
推荐指数
2
解决办法
8083
查看次数

使用java中的URLDecoder将%解码到空间?

我有一个用例,我必须解码URI的queryParameter并执行操作(超出此问题的范围).

假设我有一个URI,我必须解码它.现在我知道目前所有的%20都将被转换为空格,而创建URI 空间应该用%20来表示,但可能会出现我可能获得带%作为空间的URI的情况.因此,我想将%转换为空格以保持向后兼容性.最后有一个注释有助于理解这个问题.

我尝试过replaceall() %,%20但是后来%20又会变成%2020许多其他例外.

这是读取UPI URI所必需的,根据NPCI的官方文件:

注意:考虑到当前的PSP应用程序被开发为"%"作为空格(""),银行PSP应该同时支持"%"和"%20",直到生态系统与修订版对齐为止.因此,应确保向后兼容性.

编辑1基于pshemo评论 -

我试过了

str.replaceAll("%(?![0-9a-fA-F])","%20")
Run Code Online (Sandbox Code Playgroud)

一个不满足上述正则表达式的案例是"upi:// pay?pa = praksh%40kmbl&pn = Prakash%Abmar&cu = INR"

输出是pn - > Prakash" some othercharacter "mar

java urlencode utf-8 rfc upi

6
推荐指数
1
解决办法
173
查看次数

Springboot:通过 Resttemplate 防止 % 的双重编码

我们的代码使用 Asyncresttemplate 如下

String uri = http://api.host.com/version/test?address=%23&language=en-US&format=json

getAysncRestTemplate().getForEntity(uri, String.class);
Run Code Online (Sandbox Code Playgroud)

但是%23在 Rest 模板中进行了双重编码,%2523并且 url 变为 http://api.host.com/version/test?address=%2523&language=en-US&format=json,但是我需要传递编码字符串,如果我传递解码数据'#',它不会编码

如何在不对 URL 进行双重编码的情况下发送此请求?

已经尝试使用 UriComponentsBuilder 避免使用 Spring 的 RestTemplate 对 URL 查询参数进行双重编码

java spring-mvc spring-boot

3
推荐指数
1
解决办法
3172
查看次数

替换URL中的符号将不起作用

我试图通过从Web服务器接收URL然后将其转换为位图图像在android中显示图像,但得到以下错误,因为符号%5C在其中.

E/Error? http:%5C/%5C/thumbs3.ebaystatic.com%5C/pict%5C/3007385805144040_5.jpg
Run Code Online (Sandbox Code Playgroud)

我试图url2.replaceAll("%5C","");摆脱这个符号,但这根本没有效果.我怎么能摆脱它,所以我有一个有效的网址.

java android http replaceall

2
推荐指数
1
解决办法
541
查看次数