0

我为 2.3 版开发了应用程序,它使用 Web 服务来验证设备用户;当我部署在三星 GT-P3100(版本 4.0.3 Icecream Sandwitch)时,我遇到了相同的应用程序连接失败;虽然我可以使用浏览器与 Web 服务通信的设备.. .请建议,我是否需要为版本 4.0.3 重新创建项目,或者选项卡是否需要 mainfest 文件中的一些特殊权限?...可能是什么原因

4

1 回答 1

0

使用此代码获取 html 内容:

public static String getHTML ( String url) throws IOException { 
            if (url == null || url.length() == 0) { 
                    throw new IllegalArgumentException("url cannot be null or empty"); 
            } 
            final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); 
            final BufferedReader buf = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
            final StringBuilder page = new StringBuilder(); 
            final String lineEnd = System.getProperty("line.separator"); 
            String line; 
            try { 
                    while (true) { 
                            line = buf.readLine(); 
                            if (line == null) { 
                                    break; 
                            } 
                            page.append(line).append(lineEnd); 
                    } 
            } finally { 
                    buf.close(); 
            } 

            try {
                    return page.toString().substring(0, page.toString().length() -1);
            } catch (Exception e) {
                    return "";
            }

    }

您需要在 AndroidManifest.xml 中添加权限

<uses-permission android:name="android.permission.INTERNET" />

也许在 android 4 和更高版本上,您需要在新线程中执行此功能,为此使用代码:

startInNewThread();

//call the fonction getHTML() in a new thread
private void startInNewThread() {

            new Thread(new Runnable() {
                    @Override
                    public void run() {
                          getHTML ("URL");

                    }
            }).start();
    }

如果您需要使用“getHtml()”的结果更新 UI,则必须创建一个处理程序

于 2012-05-30T07:09:02.590 回答