emanuelx Posted September 23, 2012 at 10:55 PM Report #476313 Posted September 23, 2012 at 10:55 PM boas pessoal eu tenho aqui um problema e não sei como resolver. eu uso uma classe para fazer download de ficheiros mas o url não é do tipo http://site.php?ficheiro.rar o url é http://site.php?ficheiro=download onde é precisso usar o metodo Post para fazer o download do ficheiro. mas o problema é que por vezes o ficheiro é rar e outras vezes é .zip protected String doInBackground(String... aurl) { try { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://www.legendas-zone.org/downloadsub.php"); httpPost.addHeader("Cookie", login.cooklogin); BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("sid", aurl[0]); BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("action", "download"); List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); nameValuePairList.add(usernameBasicNameValuePair); nameValuePairList.add(passwordBasicNameValuePAir); UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList); httpPost.setEntity(urlEncodedFormEntity); HttpResponse httpResponse = httpClient.execute(httpPost); long lenghtOfFile = httpResponse.getEntity().getContentLength(); FileOutputStream f = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/download/", aurl[1])); InputStream in = httpResponse.getEntity().getContent(); byte[] buffer = new byte[1024]; int len1 = 0; long total = 0; while ((len1 = in.read(buffer)) > 0) { total += len1; publishProgress("" + (int)((total*100)/lenghtOfFile)); f.write(buffer, 0, len1); } f.close(); } catch (Exception e) { Log.d("Downloader", e.getMessage()); } return null; } com calma e sem stresses
emanuelx Posted September 24, 2012 at 10:35 AM Author Report #476363 Posted September 24, 2012 at 10:35 AM eis a solução para quem estiver com o mesmo problema Header headers[] = httpResponse.getAllHeaders(); for(Header h:headers){ System.out.println("teste" + h.getName() + ": " + h.getValue()); } o nome do ficheiro com a extensão tal e qual como fosse ser trasferido do navegador está a frente da palavra content-disposition com calma e sem stresses
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now