Android Java奇怪问题构建URL

时间:2012-02-10 17:48:24

标签: java android http

因此,我正在构建一个要调用的URL以获取JSON响应,但面临一个奇怪的问题。如下所示构建URL会返回“Not found”,但出于测试目的,我只是构建了URL“http://api.themoviedb.org/3/search/person?api_key=XXX&query=brad”并且没有' t附加任何内容并返回正确的响应。也尝试不编码“文本”和相同的事情......没找到。有什么想法吗?

        StringBuilder url = new StringBuilder();
        url.append("http://api.themoviedb.org/3/search/person?api_key=XXX&query=").append(URLEncoder.encode(text, ENCODING));
        Log.v("URL", url.toString());
        try {
            HttpGet httpRequest = null;
            httpRequest = new HttpGet(url.toString());
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
            HttpEntity entity = response.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
            InputStream input = bufHttpEntity.getContent();
            String result = toString(input);  

            //JSONObject json = new JSONObject(result);
            return result;

1 个答案:

答案 0 :(得分:0)

尝试使用我下面的代码。我已经从我使用的一些代码中复制并粘贴了它,我知道它有效。可能无法解决您的问题,但我认为它值得一试。我已经编辑了一下它,现在应该复制并粘贴到你的代码中。

    HttpGet request = new HttpGet(new URI(url.toString()));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(request);
    BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    StringBuilder builder = new StringBuilder();
    for (String line = null; (line = reader.readLine()) != null;) {
        builder.append(line).append("\n");
    }
    JSONObject jResponse = new JSONObject(builder.toString());