gpt4 book ai didi

android - 声明 URL 的正确方法?

转载 作者:行者123 更新时间:2023-11-30 04:22:32 26 4
gpt4 key购买 nike

在我的应用程序中,我试图从我的网站下载一个文件,但遇到了一些问题。首先,我似乎想不出声明 URL 的正确方法。其次,当我运行应用程序时,当我告诉连接获取 InputStream 时它崩溃了。我不知道我做错了什么。我下午的大部分时间都在网上搜索,并尝试了很多方法来解决 URL 的问题,但都没有成功。

我很想知道我做错了什么,所以我们将不胜感激任何帮助。

package shc_BalloonSat.namespace;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.http.util.ByteArrayBuffer;

import android.util.Log;

public class dl_viewKML
{
String file_path = "";
String file_url;
String file_name;

void downloadFile()
{
try
{
String file_name = "data.kml";

//URL url = new URL("http://space.uah.edu");
String encodedURL = "http:////"+URLEncoder.encode("www.wktechnologies.com/shc_android_app/", "UTF-8");
URL url = new URL(encodedURL);
File file = new File(url + "/" + file_name);

long startTime = System.currentTimeMillis();
Log.d("SHC BalloonSat", "Download beginning: ");
Log.d("SHC BalloonSat", "Download url: " + url);
Log.d("SHC BalloonSat", "Downloaded file name: " + file_name);

// Open a connection to the specified URL
URLConnection conn = url.openConnection();

// Define InputStreams to read from the URLConnection.
InputStream is = conn.getInputStream();//crashes here
BufferedInputStream bis = new BufferedInputStream(is);

// Read bytes to the Buffer until there is nothing more to read(-1).
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}

// Convert the Bytes read to a String.
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("SHC BalloonSat", "Download ready in: " + ((System.currentTimeMillis() - startTime) / 1000) + " secs.");
}

catch (IOException e)
{
Log.e("log_tag", "Error: " + e.toString());
}
}
}

最佳答案

不需要用url调用URLEncoder。 URLEncoder.encode 用于对参数进行编码:

因此将您的代码编辑为:

void downloadFile()
{
try
{
String file_name = "data.kml";

//URL url = new URL("http://space.uah.edu");
String encodedURL = "http://"+"www.wktechnologies.com/shc_android_app/data.kml";
URL url = new URL(encodedURL);

// Open a connection to the specified URL
URLConnection conn = url.openConnection();

// Define InputStreams to read from the URLConnection.
InputStream is = conn.getInputStream();//crashes here
BufferedInputStream bis = new BufferedInputStream(is);

// Read bytes to the Buffer until there is nothing more to read(-1).
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}

// Convert the Bytes read to a String.
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
Log.d("SHC BalloonSat", "Download ready in: " + ((System.currentTimeMillis() - startTime) / 1000) + " secs.");
}

catch (IOException e)
{
Log.e("log_tag", "Error: " + e.toString());
}

}

关于android - 声明 URL 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9044469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com