我需要下载JSON,然后将其存储在JSONObject中.
我正在使用org.json.JSONArray.
以下是一个地方的所有代码:
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test
{
public static JSONObject getJSON()
{
try
{
URL uri = new URL("http://events.makeable.dk/api/getEvents");
HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
{
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null)
{
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuffer buffer = new StringBuffer();
try
{
String line;
while ((line = br.readLine()) != null)
{ …Run Code Online (Sandbox Code Playgroud)