HTTP Post请求:目标服务器未响应

3
嘿, 我想为喜欢的最近曲目构建一个简单的安卓应用程序。我使用user.getRecentTrack接收歌曲,并将数据和会话密钥发送到我的方法,但出现了错误:“目标服务器未响应。”有时,还会从lastfm方法中出现Error 3:track.love:“无效方法-此包中没有该名称的方法”。 这是我对track.love的Http Post请求。
HttpClient client = new DefaultHttpClient(); 
String postURL = "http://ws.audioscrobbler.com/2.0/";;
HttpPost post = new HttpPost(postURL); 
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
String signatur = makeTrackLoveSignatur(artist, title, key);
params.add(new BasicNameValuePair("method", "track.love"));
params.add(new BasicNameValuePair("track", title));
params.add(new BasicNameValuePair("artist", artist));
params.add(new BasicNameValuePair("api_key", api_key));
params.add(new BasicNameValuePair("api_sig", signatur));
params.add(new BasicNameValuePair("sk", key));
params.add(new BasicNameValuePair("format", "json"));

UrlEncodedFormEntity ent = 
new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);

HttpResponse responsePOST = client.execute(post); 
InputStream data = responsePOST.getEntity().getContent();

我还编写了一种将InputStream转换为String的方法:

BufferedReader reader = new BufferedReader(
                new InputStreamReader(stream));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Output"+sb.toString());
        return sb.toString();

什么问题?谢谢...
1个回答

1
尝试添加Content-type头:
post.addHeader("Content-type", "application/x-www-form-urlencoded");

这里有一个关于Python的类似问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接