json显示权限错误帮助请

时间:2013-07-19 08:11:26

标签: android json

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

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

第一个json工作正常。第二个这个JSONArray dataArray = data.getJSONObject(“观察”)。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>

2 个答案:

答案 0 :(得分:0)

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

READ_EXTERNAL_STORAGE

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

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

答案 1 :(得分: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"/>