POST调用失败

时间:2014-03-18 05:53:39

标签: java rest servlets post

我正在从Java内部进行POST调用,我不确定为什么它不会通过。代码如下:

String screencastStartURL = "......";                            
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);   
nameValuePairs.add(new BasicNameValuePair("stateID", s_id));
nameValuePairs.add(new BasicNameValuePair("startTime", screencastStartTime));   
nameValuePairs.add(new BasicNameValuePair("identifier", screencastId));
System.out.println("about to make postCall"); 
String postResponse = Commons.postCall(screencastStartURL, nameValuePairs);
out.println(postResponse); // out is an earlier instantiated PrintWriter

postCall方法如下:

public static String postCall(String url, List<NameValuePair> nameValuePairs)   {
        System.out.println("Within postCall method");
        String ans = "";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
          System.out.println("Within postCall method 2");
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          HttpResponse response = httpclient.execute(httppost);

          ans = (EntityUtils.toString(response.getEntity()));

        } catch (IOException e) {
          e.printStackTrace();
        }

        return ans;
    }

当我在Tomcat服务器上部署此代码时,它会在终端中打印about to make postCall,这意味着它会在POST调用之前成功执行。但是,它不打印Within postCall method这意味着调用本身出了问题?

我看到的错误消息是NullPointerException,会导致ServletException

修改

但是,当我使用POSTMAN Rest Client Chrome应用程序进行POST调用时,它会成功完成。

第二次修改

我在浏览器中看到的堆栈跟踪是

exception

javax.servlet.ServletException: Servlet execution threw an exception
root cause

java.lang.ExceptionInInitializerError
    com.bl.apps.MemoLocalRecorder.App.doGet(App.java:121)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.NullPointerException
    java.io.Reader.<init>(Reader.java:78)
    java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    com.bl.util.Commons.setURL(Commons.java:221)
    com.bl.util.Commons.<clinit>(Commons.java:150)
    com.bl.apps.MemoLocalRecorder.App.doGet(App.java:121)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

1 个答案:

答案 0 :(得分:0)

根据您发布的堆栈跟踪。错误基本上是因为静态块中的静态变量初始化

中的任何异常

下面是一些信息和一些链接,以便更多地了解这个

  

在看到异常的名称时,您可能会猜到这一点   异常与变量的初始化有关。当然是   这个错误与静态变量的初始化有关   变量可以是基本类型或用户定义类型。您可能都是   已经了解了类的静态初始化器。

http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-exceptionininitializererror-how-to-handle-exception-initializer-error/

http://craftingjava.blogspot.in/2012/06/javalangexceptionininitializererror.html