无法从资源文件夹android读取.json文件

时间:2016-07-10 17:12:00

标签: android json

我试图从asstes文件夹android读取json文件,但我无法读取它甚至我的日志没有显示读取行后任何人都告诉我什么是错的? 这是我的代码:         公共类MapsActivity扩展FragmentActivity实现OnMapReadyCallback {

        private GoogleMap mMap;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

            loadJSONFromAsset();
        }

        public String loadJSONFromAsset() {

            String json = null;
            try {

                Log.d("entry", "loadJSONFromAsset: "+json);

                InputStream is = getAssets().open("data.json");

                int size = is.available();

                byte[] buffer = new byte[size];

                is.read(buffer);

                is.close();

                json = new String(buffer, "UTF-8");

                Log.d("json", "loadJSONFromAsset: "+json);


            } catch (IOException ex) {
                Log.d("error", "loadJSONFromAsset: "+ex.toString());
                ex.printStackTrace();
                return null;
            }
            return json;


        }

    here is the log:
    07-10 22:38:53.174 9783-9783/com.nodapp D/entry: loadJSONFromAsset: null
                                                     }
    07-10 22:38:53.174 9783-9783/com.nodapp I/ViewRootImpl: CPU Rendering VSync enable = true
    07-10 22:38:53.174 9783-9783/com.nodapp W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
    07-10 22:38:53.204 9783-9783/com.nodapp W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
    07-10 22:38:53.234 9783-9783/com.nodapp W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
    07-10 22:38:53.234 9783-9783/com.nodapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d3ecf3 time:24068058
    07-10 22:38:53.314 9783-9783/com.nodapp W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection

1 个答案:

答案 0 :(得分:1)

试试这个:

public String loadJSONFromAsset() {

    String json = null;
    try {

        Log.d("entry", "loadJSONFromAsset: "+json);

        InputStream is = getAssets().open("data.json");

        StringBuilder buf=new StringBuilder();
        BufferedReader in=
        new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String str;

        while ((str=in.readLine()) != null) {
            buf.append(str);
        }

        in.close();

        json = buf.toString();

        Log.d("json", "loadJSONFromAsset: "+json);

    } catch (IOException ex) {
        Log.d("error", "loadJSONFromAsset: "+ex.toString());
        ex.printStackTrace();
        return null;
    }
    return json;
}