Java中的Google API与Struts

时间:2012-04-13 00:57:21

标签: java

如果我使用以下方法使用普通的Java项目获取Google帐户的令牌,那么它的工作非常正常,但如果我在Struts项目中使用相同的代码,它会给我一个ClassNotFoundException。有人可以告诉我为什么它会抛出异常吗?我搞不清楚了。

public String Login(String username,String password){
    String token = null;
     HttpClient client = new DefaultHttpClient();
     String postURL = "https://www.google.com/accounts/ClientLogin";
     HttpPost post = new HttpPost(postURL);
     List<NameValuePair> nvps = new ArrayList<NameValuePair>();
     nvps.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));
     nvps.add(new BasicNameValuePair("Email", username));
     nvps.add(new BasicNameValuePair("Passwd", password));
     nvps.add(new BasicNameValuePair("service", "cp"));// Name of Google service 
     nvps.add(new BasicNameValuePair("source", "feras-MeAndTheGreenRobot-1.0"));
     UrlEncodedFormEntity ent;
     try {
             ent = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
             post.setEntity(ent);
             HttpResponse responsePOST = client.execute(post);
             HttpEntity resEntity = responsePOST.getEntity();
             if (resEntity != null) {
                      String authToken=  EntityUtils.toString(resEntity);
                     System.out.println("RESPONSE is "+ authToken );
 token = authToken.substring(authToken.indexOf("Auth=") + 5, authToken.length()).trim();
                     System.out.println("Auth is "+  authToken.substring(authToken.indexOf("Auth=") + 5, authToken.length()).trim());
             }
     } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
     } catch (ClientProtocolException e) {
             e.printStackTrace();
     } catch (IOException e) {
             e.printStackTrace();
     }
    return token;
}

异常

java.lang.ClassNotFoundException: org.apache.http.client.ClientProtocolException
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
at com.struts.action.LoginAction.submit(LoginAction.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:184)

1 个答案:

答案 0 :(得分:2)

您是否在您的网络项目的"WEB-INF/lib"文件夹下放置了HttpClient使用的jar库文件?