Bonjour à tous,
J'ai un programme qui récupère du JSON et qui le parse.
Mais je voudrais présenter mes données parsées sur mes pages jsp. Mais je ne vous pas du tout comment faire...
Merci
ma classe GetRequest()
package request;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import biz.source_code.base64Coder.Base64Coder;
public class GetRequest {
ToParse parse = new ToParse();
public GetRequest(){
HttpURLConnection connection = null;
String APIKey = "code";
String TeamworkURL = "url.com";
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host",8080));
Authenticator authenticator = new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return (new PasswordAuthentication("user",
"mdp".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
try {
URL url = new URL( TeamworkURL + "/tasks.json");
connection = (HttpURLConnection) url.openConnection(proxy);
connection.setRequestMethod("GET");
String userpassword = APIKey + ":" + "";
String encodedAuthorization = Base64Coder.encodeString( userpassword );
connection.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream responseStream = connection.getInputStream();
String jsonToParse = streamToString( responseStream);
parse.Parsing(jsonToParse);
} catch(Exception e) {
System.out.println( "Error Received:" + e.getMessage() );
}
}
public String streamToString(InputStream in) throws IOException {
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
for(String line = br.readLine(); line != null; line = br.readLine())
out.append(line);
br.close();
return out.toString();
}
}
Ma classe ToParse()
package request;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class ToParse {
public void Parsing(String jsonToParse) throws Exception{
JSONParser parse = new JSONParser();
JSONObject jobj = (JSONObject)parse.parse(jsonToParse);
JSONArray jsonarr_1 = (JSONArray) jobj.get("todo-items");
for(int i=0;i<jsonarr_1.size();i++)
{
JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
System.out.println("\n content : " +jsonobj_1.get("content"));
//Store the JSON object in JSON array as objects (For level 2 array element i.e Address Components)
JSONArray jsonarr_2 = (JSONArray) jsonobj_1.get("subTasks");
for(int j=0;j<jsonarr_2.size();j++)
{
JSONObject jsonobj_2 = (JSONObject) jsonarr_2.get(j);
String str_data1 = (String) jsonobj_2.get("content");
System.out.println(" sous tâches : " + str_data1);
}
}
}
}
Et ma servlet : Application.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import biz.source_code.base64Coder.Base64Coder;
import request.GetRequest;
public class Application extends HttpServlet {
public void doGet ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
this.getServletContext().getRequestDispatcher("/WEB-INF/Application.jsp").forward(request, response);
}
}
et pour finir ma jsp : Application.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Application</title>
</head>
<body>
</p> app <p>
</body>
</html>