0

I'm using AsyncTask to fetch information from a website for a small learning project I'm doing. I've tried my code, and it "tries" do to what I intend it to do, though I have a small problem.

I'm passing a String as parameter to execute in AsyncTask, but the parameter is referenced by address instead of value. Why is this, and how do I pass a parameter between the UI thread and the background thread properly?

This is my code where the parameter is passed:

ibSearch.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Editable artnrInput = input.getText();
            String artnr = artnrInput.toString();
            new Connection().execute(artnr);
        }
    });

I've debugged it so far and the parameter "artnr" is a value here.

This is part of my AsyncTask where the error occurs:

public class Connection extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... artnr)  {
        String url = "http://mobil.systembolaget.se/SokDryck/SokDryck.aspx?artnr="+ artnr + "&lan=01";

What I want my String url to look like is this for example: http://mobil.systembolaget.se/SokDryck/SokDryck.aspx?artnr=221101&lan=01

What it does like when I debug it, making it not work is this:

 http://mobil.systembolaget.se/SokDryck/SokDryck.aspx?artnr=[Ljava.lang.String;@43e4c9f8&lan=01

I have tried a simple toString(), but it was more of a guess. Why is it not passed as a value, but a reference?

Thank you, Z

Daniel B
  • 8,770
  • 5
  • 43
  • 76
  • The reference to the string is passed by value. Objects are not passed at all in Java, let alone by value or by reference. – user207421 Nov 22 '12 at 21:30

2 Answers2

4

You should use artnr[0] (which is the only parameter you are passing) instead of artnr (since String... artnr is an array of String objects not a single String object):

String url = "http://mobil.systembolaget.se/SokDryck/SokDryck.aspx?artnr="+ artnr[0] + "&lan=01";

Aside to that, see How Java passes objects as arguments by value

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

String... artnr is a var-args, and is actually an array only. When you print artnr, you are printing value of your array reference.

You need to change your url string to print artnr[0] instead of artnr to print the string: -

String url = "http://mobil.systembolaget.se/SokDryck/SokDryck.aspx?artnr=" + 
             artnr[0] + "&lan=01";
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • @Zyril. It's useful to know that Java passes *everything* by value. "Reference" is a bad word when talking about objects since they are more accurately pointers. An object "passed" to a method (which cannot be done) is actually passing the value of a pointer. That value is a copy of the actual pointer, which is easy to demonstrate by "passing" an object to a Java method then trying to change that object. You can modify it's properties but you cannot change the object. EDIT I just spotted the link in Eng.Fouads answer which is a great explanation of this. – Simon Nov 22 '12 at 22:25
  • @Simon.. Breathe man breathe. Actually you are right. Edited answer. :) – Rohit Jain Nov 22 '12 at 22:27
  • No worries actually I was pinging Zyril, it wasn't a comment on your answer which I think is fine. I'm wiser than to tangle with a 15.6K ;) – Simon Nov 22 '12 at 22:29
  • @Simon.. haha :) No, it's not that you cannot tangle with a 15.6K. Of course people are not judged by their reps on SO. Everyone can be wrong. And actually, I wasn't offended at all by your comment. So no worries. Cheers :) – Rohit Jain Nov 23 '12 at 03:40