如何在 android 中无错误地传递字符串 - 在附加堆栈跟踪中获取资源但从未释放 -

How to pass string in android without error- A resource was acquired at attached stack trace but never released-

我尝试将 stringBuilder 对象提取到我的主 activity。我检查了我的 json 文件或解析代码,它们运行良好。但是,当我尝试获取 stringbuilder 时出现错误: 资源已在附加堆栈跟踪中获取但从未释放。有关避免资源泄漏的信息,请参阅 java.io.Closeable。 java.lang.Throwable: 未调用显式终止方法 'close'

Server.java的代码如下

`

public class Server extends Activity {

     static StringBuilder stringBuilder = new StringBuilder();
public Server(){
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray project = obj.getJSONArray("project");

        for (int i = 0; i < project.length(); i++) {
            JSONObject ss = project.getJSONObject(i);
            stringBuilder.append(ss.getString("title") + "\n");
            JSONArray post = ss.getJSONArray("posts");

            for(int j = 0; j < post.length();j++){
                JSONObject posts = post.getJSONObject(j);
                stringBuilder.append(posts.getString("id") +"\n");
                JSONArray tag = posts.getJSONArray("tags");

                for(int k = 0; k < tag.length();k++){
                    stringBuilder.append(tag.getString(k) +"\n");
                }
            }
        }
    }
    catch (JSONException e) {
        stringBuilder.append("error");
        e.printStackTrace();
    }

}

public String getString(){

    return stringBuilder.toString();
}


public String loadJSONFromAsset() {
    String json = null;
    try {

        InputStream is = getAssets().open("cat.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;
}}

这是我的 MainActivity.java

public class MainActivity extends Activity  {


TextView jsonDataTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    jsonDataTextView = (TextView) findViewById(R.id.textView);

    Server s = new Server();
    jsonDataTextView.setText(s.stringBuilder.toString());} }

有什么解决办法吗?

InputStream 可能不会在您的 loadJSONFromAsset 方法的 try 块中关闭,它应该关闭。

public String loadJSONFromAsset() {
    String json = null;
    InputStream is = null;
    try {
        is = getAssets().open("cat.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        json = new String(buffer, "UTF-8");
    }
    catch (IOException ex) {
        ex.printStackTrace();
    }
    finally {
        if (is != null) {
            try {
                is.close();
            }
            catch (IOException ex) {
                // Do you want to handle this exception?
            }
        }
    }
    return json;
}

注意:在你的 Server 构造函数中有些东西困扰我,在 catch 块中你试图将 "error" 附加到你的 StringBuilder。你知道吗,这里的 StringBuilder 可能不为空。事实上,在 try 块中,一次(或多次)尝试向其附加一些字符串可能会在某些时候出现问题之前起作用。

注 2:服务器作为非 Activity

public class Server {

    private Context mContext;
    public Server(Context context) {
        mContext = context;
        ...
    }
    ...
    public String loadJSONFromAsset() {
        ...
            mContext.getAssets().open("cat.json");
    }
}

然后在您的 MainActivity

Server s = new Server(this);