简体   繁体   English

无法从资产文件夹Android读取.json文件

[英]Cannot read .json file from assets folder android

I am trying to read json file from asstes folder android but i am not able to read it and even my logs are not getting displayed after read line can anyone tell me whats wrong ? 我正在尝试从asstes文件夹android中读取json文件,但我无法读取它,即使在读取行后我的日志也无法显示,任何人都可以告诉我怎么了吗? here is my code: public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 这是我的代码:公共类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

Try this: 尝试这个:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM