Created
June 16, 2020 14:55
-
-
Save ottuzzi/213af1d2cb144d78e8a334a29fbd11df to your computer and use it in GitHub Desktop.
Don't do this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ejb3.webservice; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
public class jsonRead { | |
public StringBuffer read(String inputUrl) throws JSONException { | |
URL url = null; | |
HttpURLConnection con = null; | |
BufferedReader in = null; | |
String inputLine = ""; | |
StringBuffer content = new StringBuffer(); | |
try { | |
url = new URL(inputUrl); | |
} catch (MalformedURLException e) { | |
System.out.println("ERRORE1: " + e); | |
} | |
try { | |
con = (HttpURLConnection) url.openConnection(); | |
} catch (IOException e) { | |
System.out.println("ERRORE2: " + e); | |
} | |
try { | |
con.setRequestMethod("GET"); | |
} catch (ProtocolException e) { | |
System.out.println("ERRORE3: " + e); | |
} | |
con.setRequestProperty("Content-Type", "application/json"); | |
try { | |
int status = con.getResponseCode(); | |
} catch (IOException e) { | |
System.out.println("ERRORE4: " + e); | |
} | |
try { | |
in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
} catch (IOException e) { | |
System.out.println("ERRORE5: " + e); | |
} | |
try { | |
while ((inputLine = in.readLine()) != null) { | |
content.append(inputLine); | |
} | |
} catch (IOException e) { | |
System.out.println("ERRORE6: " + e); | |
} finally { | |
try { | |
in.close(); | |
} catch (IOException e) { | |
System.out.println("ERRORE7: " + e); | |
} | |
} | |
con.disconnect(); // end connection | |
return content; | |
} | |
public JSONObject jsonParser(StringBuffer buff) throws JSONException { | |
JSONObject obj = new JSONObject(buff.toString()); | |
return obj; | |
} | |
public JSONArray jsonArrayParser(StringBuffer buff) throws JSONException { | |
JSONArray jsonArray = new JSONArray(buff.toString()); | |
return jsonArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment