Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在查询字符串中发送JSON_Java_Json_Facebook_Httpclient - Fatal编程技术网

Java 在查询字符串中发送JSON

Java 在查询字符串中发送JSON,java,json,facebook,httpclient,Java,Json,Facebook,Httpclient,我试图使用facebook登录到我的web应用程序,它工作正常,直到最近我开始出现这个错误 java.net.URISyntaxException: Illegal character in query at index 43: https://graph.facebook.com/me?access_token={"access_token":"E","token_type":"bearer"} 我试图用下面的公式来解决这个问题 public static void main(String[

我试图使用facebook登录到我的web应用程序,它工作正常,直到最近我开始出现这个错误

java.net.URISyntaxException: Illegal character in query at index 43: https://graph.facebook.com/me?access_token={"access_token":"E","token_type":"bearer"}
我试图用下面的公式来解决这个问题

public static void main(String[] args) {
        String accessToken = "{\"access_token\":\"E\",\"token_type\":\"bearer\"}";
        try {

            //String urlStr = "https://graph.facebook.com/me?access_token=" + URLEncoder.encode(accessToken,"UTF-8");
            String urlStr = "https://graph.facebook.com/me?access_token=" +accessToken;
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(urlStr);
            httpget.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

试图使用
urlcoder
并将头设置为
application/json
,但仍然不起作用,可能是什么问题?

发现了问题,似乎我现在只能从json中提取访问令牌并单独发送,我已经通过

private String getUserMailAddressFromJsonResponse(String accessToken, HttpSession httpSession) {

        HttpClient httpclient = new DefaultHttpClient();
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        try {

            if (accessToken != null && !"".equals(accessToken)) {
                json = (JSONObject) parser.parse(accessToken);
                String newUrl = "https://graph.facebook.com/me?access_token=" + json.get("access_token");
                httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(newUrl);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httpget, responseHandler);
...
私有字符串getUserMailAddressFromJsonResponse(字符串accessToken,HttpSession HttpSession){
HttpClient HttpClient=新的DefaultHttpClient();
JSONParser=新的JSONParser();
JSONObject json=null;
试一试{
如果(accessToken!=null&&!“.equals(accessToken)){
json=(JSONObject)parser.parse(accessToken);
字符串newUrl=”https://graph.facebook.com/me?access_token=“+json.get(“访问令牌”);
httpclient=新的DefaultHttpClient();
HttpGet-HttpGet=newhttpget(newUrl);
ResponseHandler ResponseHandler=新BasicResponseHandler();
字符串responseBody=httpclient.execute(httpget,responseHandler);
...

为什么不将其放入请求正文中?生成错误的请求,尝试使用不同的头和编码组合,但失败的具体位置在哪里?如何呈现与该异常相关的堆栈跟踪?您首先提出了一个关于
URISyntaxException
的问题,但呈现了
HttpResponse的堆栈跟踪异常
。唯一有意义的方法是前者是否是后者包装的“原因”。在任何情况下,堆栈跟踪都必须包括描述任何和所有包装异常的部分。在任何情况下,通用URI语法和特定URL语法都不允许使用字符
{
}
,或
显示在逐字查询字符串中,因此至少必须在
access\u token
查询参数的值中对这些字符进行URL编码。尽管您的程序在执行此操作时可能仍然无法工作,但在该事件中是否真的会产生相同的异常?
org.apache.http.client.HttpResponseException: Bad Request
    at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:65)
    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:51)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
    at com.simsarak.servlet.facebook.FacebookLogin.main(FacebookLogin.java:178)
private String getUserMailAddressFromJsonResponse(String accessToken, HttpSession httpSession) {

        HttpClient httpclient = new DefaultHttpClient();
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        try {

            if (accessToken != null && !"".equals(accessToken)) {
                json = (JSONObject) parser.parse(accessToken);
                String newUrl = "https://graph.facebook.com/me?access_token=" + json.get("access_token");
                httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(newUrl);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httpget, responseHandler);
...