Java 在servlet中创建json对象时出错

Java 在servlet中创建json对象时出错,java,json,servlets,Java,Json,Servlets,在我将这个servlet上传到我的服务器后,它给出了一个错误,没有页面可以呈现给浏览器,响应与下面给出的错误相同。如果我在这个servlet中注释json objetc部分。服务器响应工作正常 浏览器显示错误 错误 servlet: import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONException; import java.io.*; import java.io.BufferedReader

在我将这个servlet上传到我的服务器后,它给出了一个错误,没有页面可以呈现给浏览器,响应与下面给出的错误相同。如果我在这个servlet中注释json objetc部分。服务器响应工作正常

浏览器显示错误

错误 servlet:

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FBOAuth extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    String code = request.getParameter("code");
    if (code == null || code.equals("")) {
    }

    String token = null;
    try {
        String g = "https://graph.facebook.com/oauth/access_token?client_id=187359628094571&redirect_uri=" + URLEncoder.encode("http://proshelf-sivagnanam.rhcloud.com/fb.do", "UTF-8") + "&client_secret=3bada871309843a626e9ca4c1cd76fc8&code=" + code;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");
        in.close();
token = b.toString();
        if (token.startsWith("{"))
            throw new Exception("error on requesting token: " + token + " with code: " + code);
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String graph = null;
    try {
        String g = "https://graph.facebook.com/me?" + token;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");
        in.close();
        graph = b.toString();
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String facebookId = null;
    String firstName = null;
    String middleNames = null;
    String lastName = null;
    String email = null;
    try {
        JSONObject json = new JSONObject(graph);
        facebookId = json.getString("id");
        firstName = json.getString("first_name");
        if (json.has("middle_name"))
           middleNames = json.getString("middle_name");
        else
            middleNames = null;
        if (middleNames != null && middleNames.equals(""))
            middleNames = null;
        lastName = json.getString("last_name");
        email = json.getString("email");
    } catch (JSONException e) {
}
}

在您的代码中,这个错误发生在哪里?错误实际上是当我尝试访问应用程序中的任何页面时,它给出了我上面提到的错误。没有内容呈现到浏览器中,这是我提到的唯一错误。在Chrome上,右键单击浏览器,单击“检查元素”,然后单击“网络”。刷新页面并单击浏览器向servlet请求的条目。向us.GET/HTTP/1.1主机显示请求和响应头:proshelf-sivagnam.rhcloud.com连接:keep-alive-Accept:text/html、application/xhtml+xml、application/xml;q=0.9,图像/webp,/;q=0.8用户代理:Mozilla/5.0(X11;Linux i686)AppleWebKit/537.36(KHTML,像Gecko)Ubuntu Chromium/30.0.1599.114 Chrome/30.0.1599.114 Safari/537.36接受编码:gzip,deflate,sdch接受语言:en-US,en;q=0.8响应头查看解析响应:HTTP/1.1 404未找到日期:2014年4月8日星期二16:31:01 GMT服务器:Apache Coyote/1.1内容长度:0保持活动:超时=15,最大=100连接:保持活动内容类型:文本/明文在代码中的何处打印JSON字符串?我看不到PrintWriter.out()的任何调用实例。
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import java.io.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FBOAuth extends HttpServlet {

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    String code = request.getParameter("code");
    if (code == null || code.equals("")) {
    }

    String token = null;
    try {
        String g = "https://graph.facebook.com/oauth/access_token?client_id=187359628094571&redirect_uri=" + URLEncoder.encode("http://proshelf-sivagnanam.rhcloud.com/fb.do", "UTF-8") + "&client_secret=3bada871309843a626e9ca4c1cd76fc8&code=" + code;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");
        in.close();
token = b.toString();
        if (token.startsWith("{"))
            throw new Exception("error on requesting token: " + token + " with code: " + code);
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String graph = null;
    try {
        String g = "https://graph.facebook.com/me?" + token;
        URL u = new URL(g);
        URLConnection c = u.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
        String inputLine;
        StringBuffer b = new StringBuffer();
        while ((inputLine = in.readLine()) != null)
            b.append(inputLine + "\n");
        in.close();
        graph = b.toString();
    } catch (Exception e) {
            // an error occurred, handle this
    }

    String facebookId = null;
    String firstName = null;
    String middleNames = null;
    String lastName = null;
    String email = null;
    try {
        JSONObject json = new JSONObject(graph);
        facebookId = json.getString("id");
        firstName = json.getString("first_name");
        if (json.has("middle_name"))
           middleNames = json.getString("middle_name");
        else
            middleNames = null;
        if (middleNames != null && middleNames.equals(""))
            middleNames = null;
        lastName = json.getString("last_name");
        email = json.getString("email");
    } catch (JSONException e) {
}
}