gpt4 book ai didi

java - HTTP 发布请求 : target server failed to response

转载 作者:太空狗 更新时间:2023-10-29 14:33:04 25 4
gpt4 key购买 nike

嘿,我想构建一个简单的 android 应用程序来喜爱最近的轨道。我通过 user.getRecentTrack 接收歌曲并将数据和 session key 发送到我的方法,但出现错误:“目标服务器未能响应。”有时,lastfm 方法也会出现错误 3:track.love:“无效方法 - 此包中没有具有该名称的方法”。这是我对 track.love 的 Http 发布请求

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();

有什么问题?谢谢...

最佳答案

尝试添加 Content-type header :

post.addHeader("Content-type", "application/x-www-form-urlencoded");

这是一个 similar question in python .

关于java - HTTP 发布请求 : target server failed to response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4707015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com