-4

这是我的代码(如下),它在日志中给出了这个错误

“java.lang.RuntimeException:无法打开跟踪文件'/sdcard/JsonObject.trace':权限被拒绝”

第一个 json 工作正常。其次,这个 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>
4

2 回答 2

0

您应该请求访问 SD 卡的权限:

READ_EXTERNAL_STORAGE

但它不应该是真正需要的。你检查过路径是否正确吗?

此外,我只看到您尝试从 RAW 存储中读取 JSON 文件:R.raw.json这不在 SD 卡上。

于 2013-07-19T08:14:18.757 回答
0

将此添加到您的清单中:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2013-07-19T08:15:11.477 回答