Android 声明URL的正确方法?
在我的应用程序中,我试图从我的网站下载一个文件,但我遇到了一些问题。首先,我似乎无法找到正确的方法来声明URL。其次,当我运行应用程序时,当我告诉连接获取InputStream时,它会崩溃。我不知道我做错了什么。我在网上搜索了一下午的大部分时间,尝试了很多方法来解决URL的问题,但都没有成功 我很想知道我做错了什么,所以任何帮助都将不胜感激Android 声明URL的正确方法?,android,file,url,download,Android,File,Url,Download,在我的应用程序中,我试图从我的网站下载一个文件,但我遇到了一些问题。首先,我似乎无法找到正确的方法来声明URL。其次,当我运行应用程序时,当我告诉连接获取InputStream时,它会崩溃。我不知道我做错了什么。我在网上搜索了一下午的大部分时间,尝试了很多方法来解决URL的问题,但都没有成功 我很想知道我做错了什么,所以任何帮助都将不胜感激 package shc_BalloonSat.namespace; import java.io.BufferedInputStream; import
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());
}
}
不需要使用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());
}
}
使用字符串encodedURL=”http://www.wktechnologies.com/shc_android_app/";
对于您应用的编码不正确,请参阅RFC3986()的第3节。它告诉您如何对URI的各个部分进行编码。不幸的是,URI的每个部分(主机、路径、查询等)的编码规则略有不同 使用字符串encodedURL=”http://www.wktechnologies.com/shc_android_app/";
对于您应用的编码不正确,请参阅RFC3986()的第3节。它告诉您如何对URI的各个部分进行编码。不幸的是,URI的每个部分(主机、路径、查询等)的编码规则略有不同 确保在您的清单中,您已声明允许访问internet 您可以使用以下命令
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());
}
}
请确保在您的清单中,您已声明允许访问internet 您可以使用以下命令
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());
}
}
你能发布StackStrace吗?你能发布StackStrace吗?好的,我这样做了,现在它给了我一个错误:error:java.io.FileNotFoundException:/http:/www.wktechnologies.com/shc\u android\u app/data.kml是什么导致http://更改为/http:/?我是说这是一个硬编码的URL。它到底是如何变化的呢?根据我的catch语句,url显示的是以/http://开头的链接,而不是http://Ok,我这样做了,现在它给了我一个错误:error:java.io.FileNotFoundException:/http:/www.wktechnologies.com/shc_android\u app/data.kml是什么导致http://更改为/http:/?我是说这是一个硬编码的URL。它到底是如何变化的呢?根据我的catch语句,url显示的是以/http:/开头的链接,而不是http://