これは私のコードです(以下)、ログにこのエラーが表示されます
「java.lang.RuntimeException: トレース ファイル '/sdcard/JsonObject.trace' を開けません: アクセス許可が拒否されました」
最初の json は正常に動作します。2 番目に、この JSONArray dataArray = data.getJSONObject("observations").getJSONArray( json 解析が問題を引き起こしています。どうすればよいでしょうか?助けてください。
public class fifthstep extends Activity {
// ImageButton imgNews, imgContact, imgSetting;
// ListView listMainMenu;
// MainMenuAdapter mma;
private HorizontalListView listview;
//private HorizontalView listview;
CategoryListAdapter2 cla;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
String CategoryAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenulist);
// imgNews = (ImageButton) findViewById(R.id.imgNews);
// imgContact = (ImageButton) findViewById(R.id.imgContact);
listview = (HorizontalListView) this.findViewById(R.id.listview);
cla = new CategoryListAdapter2(fifthstep.this);
// listview.setAdapter(mAdapter);
CategoryAPI = Utils.CategoryAPI;
// clearData();
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),
15000);
HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(CategoryAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new
InputStreamReader(atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null){
str += line;
}
JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("worldpopulation");
for (int i = 0; i < data.length(); i++) {
JSONObject object = data.getJSONObject(i);
// JSONObject category =
object.getJSONObject("Category");
Category_ID.add(Long.parseLong(object.getString("rank")));
Category_name.add(object.getString("name"));
Category_image.add(object.getString("url"));
Log.d("Category name", Category_name.get(i));
listview.setAdapter(cla);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
// IOConnect = 1;
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onStart() {
super.onStart();
// Downloading the RSS feed needs to be done on a separate thread.
Thread downloadThread = new Thread(new Runnable() {
public void run() {
Debug.startMethodTracing("JsonObject");
try {
loadData();
} catch (Exception e) {
Log.e("Content Retriever",
e.getLocalizedMessage(), e);
} finally {
Debug.stopMethodTracing();
}
}
}, "Reading Thread");
downloadThread.start();
}
/**
* Loads the sample JSON data into a table.
*
* @throws JSONException
* @throws IOException
*/
private void loadData() throws JSONException, IOException {
populateTable(getContent());
}
/**
* Reads the data into a {@link JSONObject}.
*
* @return the data as a {@link JSONObject}
* @throws IOException
* @throws JSONException
*/
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;
}
}
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/localTime"
android:textColor="#ffffff"
android:gravity="left" />
<TextView
android:id="@+id/apprentTemp"
android:textColor="#ffffff"
android:gravity="center" />
</TableRow>