【问题标题】:WebView download listener is giving Target host must not be null, or set in parameters errorWebView 下载侦听器正在给目标主机不能为空,或在参数中设置错误
【发布时间】:2012-02-16 12:19:50
【问题描述】:

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

我的代码是……

// 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());
        }

   }
});

【问题讨论】:

    标签: android http xmlhttprequest httpresponse


    【解决方案1】:

    尝试使用:

    将“http://”添加到“api.i-v-o.ch”

    所以应该是:“http://api.i-v-o.ch” 大多数情况下它会帮助你......

    【讨论】:

      猜你喜欢
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多