0

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

  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;

}

任何帮助将不胜感激,这已经困扰我好几天了。

4

2 回答 2

0

好吧,如果其他人遇到同样的问题,请改为关注

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

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

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

于 2012-06-11T08:31:22.460 回答
0

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

例如:

  InputStream is = null;
  JSONObject jObj = null;
  String json = null;
于 2012-06-11T09:08:35.277 回答