0

我正在尝试从本地服务器下载一个包,我为此传递了正确的 url,但它给出了异常,因为 IllegalStateException,目标主机不能为空或在参数中设置。

我的代码是

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


                }
            });
        }

    }
4

1 回答 1

0

如果您正在将文件下载到 SD 卡,请确保您的 sdcard 已安装

试试这个下载文件:

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

        }
于 2012-04-19T05:49:53.943 回答