0

Ok, I have looked at multiple examples and my code seems to be correct, but for what ever reason the parameters are not being added to the URL. I'm trying to connect to the Last.Fm API and my code is as follows:

searchIT.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            //Create new HTTPClient and Post header
            HttpPost API_ROOT = new HttpPost("http://ws.audioscrobbler.com/2.0/");
            HttpClient httpclient = new DefaultHttpClient();

            try 
            {
                //Add Data
                List<NameValuePair> nameValPairs = new ArrayList<NameValuePair>(4);

                nameValPairs.add(new BasicNameValuePair("method", "artist.getevents")); //Get events for artist
                nameValPairs.add(new BasicNameValuePair("artist", namesBox.getText().toString()));  //Get artist name
                nameValPairs.add(new BasicNameValuePair("autocorrect", "1"));   //Turn on AutoCorrect
                nameValPairs.add(new BasicNameValuePair("api_key", "xxxxxxxxxxxxxxxxxxxx")); //API Key - redacted for privacy
                API_ROOT.setEntity(new UrlEncodedFormEntity(nameValPairs));
                Log.i(TAG, "URL: " + API_ROOT.getURI().toString());

                //Execute HTTP Post Request
                HttpResponse response = httpclient.execute(API_ROOT);


            } 
            catch (UnsupportedEncodingException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

Am I just missing something here? In the log the url is shown as: http://ws.audioscrobbler.com/2.0/ so it seems to missing all the parameters I was trying to pass into it. Any ideas, tips, corrections or suggestions? Thanks!

Ron
  • 24,175
  • 8
  • 56
  • 97
Tom
  • 183
  • 3
  • 14

1 Answers1

0

From your onClick method start a new activity and pass all the parameters you want to as

            HashMap<String, String> o = (HashMap<String, String>) searchList.getItemAtPosition(position);
            Intent i = new Intent(SearchActivity.this, SearchDetails.class);
            i.putExtra("method", o.get("method"));
            i.putExtra("artist", o.get("artist"));
            i.putExtra("autocorrect", o.get("autocorrect"));
            i.putExtra("api_key", o.get("api_key"));

Then in your new activity: In the onCreate method

    Intent myIntent = getIntent(); 
    String method= myIntent.getStringExtra("method");
    String artist= myIntent.getStringExtra("artist");
    String autocorrect= myIntent.getStringExtra("autocorrect");
    String api_key= myIntent.getStringExtra("api_key");

Now you'll get the params as you require and add them to your URL.

Hope it works

ReNa
  • 1,114
  • 6
  • 19
  • 37
  • Nice suggestion and I had thought about doing it this way, right now though I'm just testing and have 1 activity where the user enters the band name and hits search so not sure Intents would really come into play here, but I do plan on using more activities later...but I digress. My main concern would be that the parameters being passed into the URL change depending on what you are requesting (i.e. the "artist" parameter isn't always in there). Seems to me you would want to have once method for calling the API and pass in the parameters into that method depending on what you are trying to do. – Tom Jun 08 '11 at 05:00