首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WebView下载侦听器提供的目标主机不能为空,或在参数错误中设置

WebView下载侦听器提供的目标主机不能为空,或在参数错误中设置
EN

Stack Overflow用户
提问于 2012-02-16 20:19:51
回答 1查看 1.2K关注 0票数 2

在我的WebView中,我试图从一些网站下载一些文件,但在执行http响应时,它给出了一个错误,说是"Target host must not be null,"

我的代码是..。

代码语言:javascript
运行
复制
// TODO: Download Listener
    webview.setDownloadListener(new DownloadListener() {

    @Override
    public void onDownloadStart(String url, String sUserAgent,
        String contentDisposition, String mimetype,
            long contentLength) {

    if (sUserAgent == null) {
    Log.e(TAG + " - Conexion", "Error on useragent");
            }

    String PATH = Environment.getExternalStorageDirectory()
                    + "/download/";
        Log.v("", "PATH: " + PATH);
    File file = new File(PATH);
        if (file.mkdirs()) {
    System.out.println("Directry-->" + PATH + " is created");
        }

    try {
        mUrl = new URL(url);
        } catch (MalformedURLException e2) {
            e2.printStackTrace();
        }
        String fileName = getFileName(mUrl);

    File outputFile = new File(file, fileName);
    try {
        fos = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e1) {

        e1.printStackTrace();
        }

    client = new DefaultHttpClient();

    try {
        HttpPost request = new HttpPost(URLEncoder.encode(url, "UTF-8"));
    } catch (UnsupportedEncodingException e1) {

        e1.printStackTrace();
            }
    request.setHeader("User-Agent", sUserAgent);
    try {
        response = client.execute(request);

        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HTTP_STATUS_OK) {

        Toast.makeText(myApp, "Error On Download",Toast.LENGTH_SHORT).show();
    } else {

          InputStream in = response.getEntity().getContent();
      byte[] read = new byte[1024];
      int numReadBytes = 0, singleByte;
        boolean endFlow = false;
        do {
            singleByte = in.read();
            endFlow = singleByte == -1;
            if (!endFlow) {
            read[numReadBytes] = (byte) singleByte;
            numReadBytes++;
            }
        } while (!endFlow);
        if (numReadBytes > 0) {

        fos.write(read, 0, numReadBytes);

        }
    }
    } catch (IOException e) {
    Log.e(TAG + " - Conexion", e.getMessage());
    } catch (ArrayIndexOutOfBoundsException e) {
    Log.e(TAG + " - Conexion", e.getMessage());
        }

   }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-16 21:29:30

尝试使用:

添加"http://“to "api.i-v-o.ch”

所以它应该是:"http://api.i-v-o.ch“基本上它会帮助你……

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9311167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档