好吧,我已经看了多个例子,我的代码似乎是正确的,但无论出于什么原因,参数没有被添加到URL。我试图连接到Last.Fm API,我的代码如下:
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();
}
}
});
字符串
我只是错过了什么吗?在日志中的网址显示为:http://ws.audioscrobbler.com/2.0/,所以它似乎错过了我试图传递到它的所有参数。任何想法,提示,更正或建议?
1条答案
按热度按时间qxsslcnc1#
从onClick方法中启动一个新的Activity,并将所有需要的参数作为
字符串
然后在新Activity中:在onCreate方法中
型
现在您将获得所需的参数并将其添加到您的URL。
希望它能起作用