How does OkHttp get Json string?

try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(urls[0]) .build(); Response responses = null; try { responses = client.newCall(request).execute(); } catch (IOException e) { e.printStackTrace(); } String jsonData = responses.body().string(); JSONObject Jobject = new JSONObject(jsonData); JSONArray Jarray = Jobject.getJSONArray(“employees”); for (int i = 0; i < Jarray.length(); i++) { JSONObject object … Read more

How to add Access-Control-Allow-Origin to jetty server

Setup the org.eclipse.jetty.servlets.CrossOriginFilter in your web app. Old question/answer on the topic: https://stackoverflow.com/a/8454168/775715 See Jetty Documentation Hub on CrossOriginFilter Use: Quick Start Grab yourself a copy of jetty-servlets.jar. Put the jetty-servlets.jar in your WEB-INF/lib Add the following to your WEB-INF/web.xml <filter> <filter-name>cross-origin</filter-name> <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class> <init-param> <param-name>allowedOrigins</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>allowedMethods</param-name> <param-value>GET,POST,HEAD</param-value> </init-param> <init-param> <param-name>allowedHeaders</param-name> <param-value>X-Requested-With,Content-Type,Accept,Origin</param-value> </init-param> … Read more

Jersey fails when creating uber jar with maven-assembly-plugin

If you look inside the MOXy jar, you will see a folder META-INF/services. In that folder, you will see a file named org.glassfish.jersey.internal.spi.AutoDiscoverable. The content of that file should be a single line org.glassfish.jersey.moxy.json.internal.MoxyJsonAutoDiscoverable What this file is for is to allow Jersey to discover the MoxyJsonAutoDiscoverable, which registers MOXy for Jersey. This service loader … Read more

Serving static files from alternate path in embedded Jetty

What you need: A DefaultServlet at “https://stackoverflow.com/” (recommended, it is a requirement of the servlet spec) this should be at named dispatcher of “default” (another requirement of servlet spec) An alternate DefaultServlet with your custom static content, configured via init-params Using a different named dispatcher than “default” (to avoid a name collision between other servlet … Read more