Android - 删除JSONObject

时间:2012-06-09 20:04:53

标签: java android json

我一直在搜索Google并尝试不同的方法来尝试让它工作,但我似乎无法获得密钥 - >要从内存中删除的值。我想要实现的是,如果服务器的某些事情发生,例如服务器重新启动,那么当你向服务器发出请求时,它将作为网络错误返回。如果我停止服务器,发出请求它会很好。但是,如果我启动服务器,发出请求并收到良好的结果,而不是杀死服务器并返回,它仍然保留以前的结果,所以它没有看到问题。希望足够清楚。我的代码的一个例子是:

  JSONObject json;
  json = MF.geoLocal(address, city, state, postal);
        try {
              if(json == null || json.isNull("status")){
                    PopIt(getString(R.string.alert_network_error));
              } else {
                    if(json.getString("status").equals("OK")){
                          address = json.getString("address");
                          city = json.getString("city");
                          county = json.getString("county");
                          state = json.getString("state");
                          postal = json.getString("postal");
                          lat = json.getString("latitude");
                          lon = json.getString("longitude");
                          db.open();
                          String method = (db.isDatabaseSet())? "update" : "insert";
                          db.addUser(method, email, fname, mname, lname, address, apt, city, county, state, postal, lat, lon, phone, mobile, dob, gender);
                          db.close();
                    }
              }
        }

geoLocat的类是:

public JSONObject geoLocal(String address, String city, String state, String postal){
    JSONObject json = null;
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("address", address));
    params.add(new BasicNameValuePair("city", city));
    params.add(new BasicNameValuePair("state", state));
    params.add(new BasicNameValuePair("postal", postal));
    json = jsonParser.getJSONFromUrl(geoURL, params);
    return json;
}

解析这些请求的类是:

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

任何帮助都会受到赞赏,这已经让我的脑子好几天了。

2 个答案:

答案 0 :(得分:0)

好吧,如果有其他人遇到同样的问题,请转到以下

static InputStream is = null;
static JSONObject jObj = null;
static String json = ""; 

InputStream is = null;
JSONObject jObj = null;
String json = ""; 

将问题解决到信息未保存到内存中的位置。

答案 1 :(得分:0)

静态成员变量每个类1个,并且由类的所有对象共享。即使它的对象消失了,这个数据也保存在静态成员中,对于新创建的类对象也是如此。

例如:

  InputStream is = null;
  JSONObject jObj = null;
  String json = null;