在我的应用程序中,我正在尝试从我的网站下载文件,但我遇到了一些问题。首先,我似乎无法找出声明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());
}
}
}
答案 0 :(得分:1)
无需使用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());
}
}
答案 1 :(得分:0)
使用String encodedURL =“http://www.wktechnologies.com/shc_android_app/”; 对于您应用的编码,请参阅RFC3986(http://tools.ietf.org/html/rfc3986#section-3)的第3部分。它告诉您如何编码URI的各个部分。不幸的是,URI的每个部分(主机,路径,查询等)具有略微不同的编码规则。
答案 2 :(得分:0)
确保在您的舱单中,您已经宣布了互联网许可。
您可以使用以下
public class dl_viewKML
{
private static final String encodedURL ="http://www.wktechnologies.com/shc_android_app/";
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");
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());
}
}