[英]Android - Removing JSONObject
我一直在搜索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;
}
任何幫助將不勝感激,這已經困擾了我好幾天了。
好吧,以防萬一其他人遇到同樣的問題,請更改為
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
至
InputStream is = null;
JSONObject jObj = null;
String json = "";
將解決不將信息保存到內存中的問題。
例如:
InputStream is = null;
JSONObject jObj = null;
String json = null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.