Android:httppost索引查询中的非法字符

时间:2014-09-08 01:39:50

标签: java android facebook-graph-api

我试图解析从图表facebook返回的json,一个来自页面的提要。 网址是这样的:https://graph.facebook.com/pageId/feed?access_token=MyAppId|MySecretKey&limit=10

但是我收到错误:索引7处的查询中出现非法字符。索引77处的字符是“|”。我的代码:

private JSONObject getJSONFromUrl(String url) throws UnsupportedEncodingException {
        // Making HTTP request
        InputStream is = null;
        JSONObject jObj = null;
        String json = "";
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;
    }

如果我使用编码器替换URL,如下所示:

String url = "https://graph.facebook.com/pageId/feed?access_token=MyAppId" + URLEncoder.encode("|", "UTF-8") + "mySecretKey&limit=10";

我收到错误:

  

{“error”:{“message”:“(#200)用户未授权应用程序执行此操作”,“”“键入”:“OAuthException”,“code”:200}}

但如果我复制并粘贴原始网址,则会成功返回JSON

如何正确发送此案例的网址?

0 个答案:

没有答案