Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取web内容,获取异常_Java - Fatal编程技术网

Java 读取web内容,获取异常

Java 读取web内容,获取异常,java,Java,我正在制作一个Android应用程序,为此我必须从一个URL(由一些python脚本输出)获取JSON内容 我找到了一个这样的方法,但我无法让它工作。代码如下: public String webGet(String URL) throws Exception { BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); clie

我正在制作一个Android应用程序,为此我必须从一个URL(由一些python脚本输出)获取JSON内容

我找到了一个这样的方法,但我无法让它工作。代码如下:

public String webGet(String URL) throws Exception  
{ 
    BufferedReader in = null; 
    try  
    { 
        HttpClient client = new DefaultHttpClient(); 
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android"); 

        HttpGet request = new HttpGet();
        request.setHeader("Content-Type", "text/html; charset=utf-8"); 
        request.setURI(new URI(URL)); 

        // Crashes after executing this line
        HttpResponse response = client.execute(request); 

        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

        StringBuffer sb = new StringBuffer(""); 
        String line = ""; 

        String NL = System.getProperty("line.separator"); 
        while ((line = in.readLine()) != null)  
        { 
            sb.append(line + NL); 
        } 
        in.close(); 
        String page = sb.toString();

        //System.out.println(page); 
        return page; 
    }  
    finally  
    { 
        if (in != null)  
        { 
            try  
            { 
                in.close(); 
            }  
            catch (IOException e)     
            { 
                Log.d("BBB", e.toString()); 
            } 
        } 
    } 
}
此崩溃发生在此行之后:

HttpResponse response = client.execute(request);
然后它简单地跳转到
最后
部分,我能从异常消息中得到的只是某个东西是
null
。就这么说

有人知道问题出在哪里吗


异常描述的屏幕截图:


您应该尝试捕获异常,而不是抛出异常,这样您可以获得有关错误的更多信息

try {

} catch (ExceptionType name) {

} catch (ExceptionType name) {

}

在某些版本的Android中,您无法从“主”线程执行网络操作。这是为了确保你的应用程序仍然对最终用户做出响应,即使它执行了一些需要很长时间的阻塞、网络操作

您需要在另一个线程中执行该操作


什么是完整的异常消息?@Vakimshaar添加了错误的屏幕截图。您仍然看不到整个stacktraceIt仍然没有告诉我比以前更多的信息。有关详细信息,请参见我的屏幕截图。