0

I'm trying to download file from URL using HttpURLConnection in Android.

First I added textviews programmatically and set a listener to each textviews to download a file. Below is that code.

for(Element ele: elements){
            final TextView attachItem = new TextView(this);
            attachItem.setText("myStr");
            attachItem.setTag("myStr2");

            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            attachItem.setLayoutParams(llp);

            ll.addView(attachItem, i++);

            // set a listener to textview
            attachItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // when clicked, execute class that extends `AsyncTask`
                    new downloadAttach().
                            execute(attachItem.getTag().toString(), attachItem.getText().toString());
                }
            });
        }

And downloadAttach() downloads a file from server using http protocol. Below is code.

        HttpURLConnection con = (HttpURLConnection)(new URL("MyUrl")).openConnection();
        con.setRequestMethod("POST");
        ...
        ...
        ...
        con.setRequestProperty("Cookie", "myCookie");
        con.setDoInput(true);
        con.setDoOutput(true);

        DataOutputStream output = new DataOutputStream(con.getOutputStream());
        output.writeBytes("myQuery");
        output.close();

        InputStream is = con.getInputStream();

        FileOutputStream fos = new FileOutputStream(new File(Environment.getDataDirectory(), "fileName"));
        // In my case, getDataDirectory() returns "/data"

        byte[] buffer = new byte[1024];
        int len;

        while ((len = is.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }

        is.close();
        fos.close();

But when I click a textview, nothing changes. There isn't a file in /data directory in my phone.

What's the problem? Someone please help.

soonoo
  • 867
  • 1
  • 10
  • 35

2 Answers2

0

You want to download file in sd-card? if yes try changing path to

Environment.getExternalStorageDirectory() + filename 

or in you app package directory use "/data/packagename/" + filename.

Piyush
  • 18,895
  • 5
  • 32
  • 63
Pragati
  • 21
  • 2
  • I think you should first add log to check if the parameters transfered to downloadTask is correct and then if the downloading happened normally. – Chine Gary Feb 10 '15 at 05:42
  • @ChineGary I think parameters are transfered correctly. How can I check if the downloading happened normally? There are no exceptions at all... – soonoo Feb 10 '15 at 05:56
  • I think fos.flush() should be called before fos.close().Then you could also add some logs to record how many bytes was wrote to fos,if it's the same as the file size. – Chine Gary Feb 10 '15 at 05:57
0

You need to call #connect, otherwise the request is never made.

con.setDoInput(true);
con.setDoOutput(true);

con.connect();

DataOutputStream output = new DataOutputStream(con.getOutputStream());
Sanketh Katta
  • 5,961
  • 2
  • 29
  • 30
  • I've always used the `#connect` method, but it seems that calling`#getOutputStream` or `#getInputStream` [implicitly invokes it](http://stackoverflow.com/a/18742082/1047268). – Sanketh Katta Feb 10 '15 at 05:58