我的代码基本上是根据TMDB中的给定关键字检索搜索结果列表(API ver.3,新API)。
public String getPersonSearchResult(String keywords){
String query = URLEncoder.encode(keywords);
String TMDB_API_URL = "http://api.themoviedb.org/3/search/person?";
String TMDB_LIMIT_LIST = "&page=1";
String TMDB_QUERY = "&query=" + query;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try
{
// ATTEMPT HTTP REQUEST
String fullUrl = TMDB_API_URL + TMDB_API_KEY + TMDB_QUERY + TMDB_LIMIT_LIST;
Log.w(APP_TAG, "TRYING [" + fullUrl + "]");
response = httpclient.execute(new HttpGet(fullUrl));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
}else{
// FAILED REQUEST - CLOSE THE CONNECTION
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch(Exception e){
Log.w(APP_TAG, e.getLocalizedMessage());
Log.w(APP_TAG, "FAILED TO RETRIEVE JSON DATA");
}
return responseString;
}
问题是我总是得到406状态代码(不可接受)。当我试图自己运行URL时
http://api.themoviedb.org/3/search/person?api_key=<MY_API_KEY_HERE>&query=jennifer&page=1
它正确显示JSON结果。
我不确定为什么会这样。类似的函数用于从其他源检索JSON值,并且它可以很好地工作。
这是他们关于搜索的API文档:http://docs.themoviedb.apiary.io/#search
有人能指出我正确的方向吗?任何帮助表示赞赏。
答案 0 :(得分:0)
一种方法来做到这一点 尝试使用builder.scheme
URIBuilder builder = new URIBuilder();
builder.setScheme("http").setHost("api.themoviedb.org").setPath("/3/search/person")
.setParameter("api_key", YOURAPIKEY)
.setParameter("page", 1)
.setParameter("query", query)
URI uri = builder.build();
HttpGet httpget = new HttpGet(uri);
.....
response = httpclient.execute(new HttpGet(httpget ));
答案 1 :(得分:0)
我想通过添加这个来解决这个问题:
HttpGet getObj = new HttpGet(fullUrl);
getObj.addHeader("Accept", "application/json");
这可能是API特定要求。虽然不确定......
答案 2 :(得分:0)
我认为它与你的代码无关......你的代码是完美的。唯一的问题可能是API请求方法。也许他们需要在请求中请求一些特定的标头。试试请求标题,如“(”Accept“,”application / json“)”它可能有用......
答案 3 :(得分:0)
试试这个
String ret = null; try {
response = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
try {
ret = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
Log.e("HttpRequest", "" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
return ret;