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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think you should first add log to check if the parameters transfered to downloadTask is correct and then if the downloading happened normally.
@ChineGary I think parameters are transfered correctly. How can I check if the downloading happened normally? There are no exceptions at all...
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.
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());

1 Comment

I've always used the #connect method, but it seems that calling#getOutputStream or #getInputStream implicitly invokes it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.