好的,我查看了多个示例,我的代码似乎是正确的,但是无论出于何种原因,参数都没有添加到 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();
}
}
});
我只是在这里遗漏了什么吗?在日志中,url 显示为:http ://ws.audioscrobbler.com/2.0/所以它似乎丢失了我试图传递给它的所有参数。有任何想法、提示、更正或建议吗?谢谢!