I have been searching Google and implementing different methods to try and get this to work, but I can’t seem to get a key -> value to remove from the memory. What I am trying to achieve is if something with the server were to happen such as the server reboots, than when you make a request to the server it would come back as a network error. It works well if I stop the server, make a request. But if I start the server, make a request and it receives good results, and than kill the server and go back, it still holds the previous results so it doesn’t see a problem. Hope that is clear enough. An example of my code is:
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();
}
}
}
The Class for geoLocat is:
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;
}
And the class to parse these requests is:
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;
}
Any help would be appreciated, this has raddled my brain now for days.
Well, in case anyone else runs into this same problem, changing to following
to
Will solve the problem to where the information isn’t saved into the memory.