Android 从本地服务器下载文件时出错
我正在尝试从本地服务器下载一个包,我正在传递正确的url,但它作为IllegalStateException发出异常,目标主机不能为null或在参数中设置 我的代码是Android 从本地服务器下载文件时出错,android,local,Android,Local,我正在尝试从本地服务器下载一个包,我正在传递正确的url,但它作为IllegalStateException发出异常,目标主机不能为null或在参数中设置 我的代码是 public class HeloAndroidActivity extends Activity{ Button btn; String ss; URI i; public void onCreate(Bundle savedInstanceState) { super.onCre
public class HeloAndroidActivity extends Activity{
Button btn;
String ss;
URI i;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// final String URL="http://www.xxx.com/webservice-demo/Package name.zip";
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "hii", Toast.LENGTH_LONG).show();
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String path="http://www.xxx.com/webservice-demo/package name.zip";
try {
ss=URLEncoder.encode(path, "UTF-8");
} catch (UnsupportedEncodingException e1) {
Log.d("exception","coming");
e1.printStackTrace();
}
try {
i=new URI(ss);
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpGet httpGet = new HttpGet(i);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String result; //String result definieren
result = builder.toString(); //Hier wird der inhalt vom Stringbuilder zum String result hinzugefühgt
Log.d("packagedata", "coming " + result);
} else {
Log.e(HeloAndroidActivity.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
如果要将文件下载到SD卡,请确保已安装SD卡 尝试下载此文件:
try {
URL url = new URL(provide any URL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "packageName.zip";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
// }
} catch (IOException e) {
Log.d(LOG_TAG, "Error: " + e);
Toast.makeText(myApp, "error " + e.toString(), Toast.LENGTH_LONG)
.show();
}
我想在模拟器上下载软件包的内容。你可以在emulator中使用SD卡,你可以指导我如何在emulator中使用SD卡吗?在我之前的评论中,我已经给了你链接。如果您遇到任何问题,请在此处发布。您可以发布获得的异常或logcat