我正在开发一个与jsf应用程序通信的android应用程序,以便从数据库中获取数据,当我运行android应用程序时显示错误
com.google.gson.stream.malformedjsonexception : Use jsonReader.setLenient(true) to accept malformed json
完整错误:
05-02 16:03:51.772: E/(837): [{"description":"CAISSES","nomCat":"CAISSES","photo":"R.JPEG","idCat":1},{"description":"CAISSEDETAIL","nomCat":"CAISSEDETAIL","photo":"P.JPEG","idCat":2}]<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/pime/faces/javax.faces.resource/theme.css?ln=primefaces-hot-sneaks" /><link type="text/css" rel="stylesheet" href="/pime/faces/javax.faces.resource/fa/font-awesome.css?ln=primefaces&v=5.2.RC3" /><link type="text/css" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" /><!--[if lt IE 9]><script src="/pime/faces/javax.faces.resource/js/html5shiv.js?ln=bsf"></script><script src="/pime/faces/javax.faces.resource/js/respond.js?ln=bsf"></script><![endif]--><script type="text/javascript">if(window.PrimeFaces){}</script></head></html>
从错误中可以看出它加载了整个页面代码,包括json数据。
机器人
class myTask extends AsyncTask<String, Integer, StringBuffer> {
@Override
protected StringBuffer doInBackground(String... args) {
String url = args[0];
StringBuffer stringBuffer = getHttpResponse(url);
return stringBuffer;
}
@Override
protected void onPostExecute(StringBuffer result) {
Log.e("", result.toString());
Gson gson = new Gson();
Categorie[] categories = gson.fromJson(result.toString(), Categorie[].class);
for(Categorie c : categories){
Log.e("", c.getNomCategorie());
cats.add(c);
}
listCatModel.notifyDataSetChanged();
super.onPostExecute(result);
}
}
我从android调用的xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head></h:head>
<f:metadata>
<f:event type="preRenderView" listener="#{loginBean.authentifier}" />
</f:metadata>
</html>
支持bean的方法:
public @ResponseBody void authentifier() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext ext = context.getExternalContext();
HttpServletResponse response = (HttpServletResponse) ext.getResponse();
PrintWriter writer=response.getWriter();
JSONArray jArray = new JSONArray();
JSONObject jsonObject=null;
List<Categories> listCategories = iCategorieManager.listCategories();
for (Categories cats : listCategories) {
jsonObject=new JSONObject(cats);
jArray.put(jsonObject);
}
writer.println(jArray);
}
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Beans.xml</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>
hot-sneaks
</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>primefaces.FONT_AWESOME</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.xhtml</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers, Last-Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
<init-param>
<param-name>cors.support.credentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
答案 0 :(得分:0)
这里发生的是你正在调用JSF视图,它正在调用托管bean,它正在将json写入响应,然后JSF视图也将其html呈现给响应。这当然会导致无效的json响应。
一种选择是直接调用REST服务,而不是通过JSF视图。但是这需要你设置spring-mvc DispatcherServlet。
更快的解决方法是指示JSF完成编写响应,并且不应该呈现任何其他内容。为此,您可以在身份验证方法的最后使用FacesContext的responseComplete方法。
context.responseComplete();