嗨,这是我加载 Json 表单资源的代码我想更改此代码以从 URL 加载 Json 我该怎么办?我将如何更改此代码请帮助我。我只想加载 Json 表单 URL 请帮帮我
private JSONObject getContent() throws IOException, JSONException
{
BufferedReader bufferedReader = null;
try
{
InputStream inStream = getResources().openRawResource(R.raw.json);
BufferedInputStream bufferedStream = new BufferedInputStream(inStream);
InputStreamReader reader = new InputStreamReader(bufferedStream);
bufferedReader = new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
String line = bufferedReader.readLine();
while (line != null)
{
builder.append(line);
line = bufferedReader.readLine();
}
return new JSONObject(builder.toString());
}
finally
{
if (bufferedReader != null)
{
bufferedReader.close();
}
}
}
/**
* Populates the table in the main view with data.
*
* @param data
* the read JSON data
* @throws JSONException
*/
private void populateTable(JSONObject data) throws JSONException
{
JSONArray dataArray = data.getJSONObject("observations").getJSONArray("data");
final TableLayout table = (TableLayout) findViewById(R.id.table);
for (int i = 0; i < dataArray.length(); i++)
{
final View row = createRow(dataArray.getJSONObject(i));
table.post(new Runnable()
{
public void run()
{
table.addView(row);
}
});
}
}
/**
* Creates a row for the table based on an observation.
*
* @param item
* the JSON object containing the observation
* @return the created row
* @throws JSONException
*/
private View createRow(JSONObject item) throws JSONException
{
View row = getLayoutInflater().inflate(R.layout.rows, null);
((TextView) row.findViewById(R.id.localTime)).setText(item.getString("local_date_time_full"));
((TextView) row.findViewById(R.id.apprentTemp)).setText(item.getString("apparent_t"));
return row;
}