2012-04-19 120 views
0

我想从本地服务器上下载一个包我正在传递正确的url但它给异常作为IllegalStateException,目标主机不能为null或在参数中设置。从本地服务器下载文件时出错

我的代码是

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


       } 
      }); 
     } 

    } 
+0

请发表您的错误日志。 – Sam 2012-04-19 05:13:28

+0

可以发布你正在得到的异常或logcat – 2012-04-19 09:08:16

回答

0

如果您下载文件到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(); 

     } 
+0

我想下载软件包上的内容 – user1196969 2012-04-19 06:29:25

+0

你可以在模拟器中使用SDCard,http://www.androiddevelopment.org/2008/11/11/how-to -create-and-use-the-sd-card-with-the-android-emulator/ – Nimit 2012-04-19 10:03:53

+0

可以指导我如何在模拟器中使用SDCard – user1196969 2012-04-19 10:39:01