Json Keep Getting The Old Data
Solution 1:
You have jObj
as a class level variable, you should probably have it as a method level variable (declare it inside the getJsonFromUrl()
method) and assign it a default value or null
.
I would guess that you are catching a JSONException or one of the other connection Exceptions and the previous value for it is not getting set to the new input and then you are returning the old version or worse, a version from another method's last run.
In this case, even though errors occur either fetching or parsing the JSON you return the jObj
regardless. This means that if an error has occurred then jObj
will still be set to its last value and that gets returned instead.
The same also holds true for the other class level variables (InputStream is and String json). I would actually suggest that you remove all class level variables and make all the class' methods static
. That way you can be sure no stale data is ever returned.
Here is your class changed:
publicclassJSONParser
{
publicstatic JSONObject getJSONFromUrl(String url)
{
Stringjson=null;
JSONObjectjObj=null;
// Making HTTP requesttry
{
// defaultHttpClientDefaultHttpClienthttpClient=newDefaultHttpClient();
url += "?";
HttpGethttpGet=newHttpGet(url);
HttpResponsehttpResponse= httpClient.execute(httpGet);
json = EntityUtils.toString(httpResponse.getEntity());
Log.d("JSON", json);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// try parse the string to a JSON objecttry
{
jObj = newJSONObject(json);
}
catch (NullPointerException e)
{
Log.e("JSON Parser", "json String was null");
}
catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Stringreturn jObj;
}
publicstatic JSONObject setJSONFromUrl(String url, List<NameValuePair> params)
{
InputStreamis=null;
Stringjson=null;
JSONObjectjObj=null;
// Making HTTP requesttry
{
// defaultHttpClientDefaultHttpClienthttpClient=newDefaultHttpClient();
HttpPosthttpPost=newHttpPost(url);
httpPost.setEntity(newUrlEncodedFormEntity(params));
HttpResponsehttpResponse= httpClient.execute(httpPost);
HttpEntityhttpEntity= httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedReaderreader=newBufferedReader(newInputStreamReader(is, "iso-8859-1"), 20);
StringBuildersb=newStringBuilder();
Stringline=null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
json = sb.toString();
Log.d("JSON", json);
}
catch (Exception e)
{
Log.e("Buffer Error", "Error converting result " + e.toString());
}
finally
{
// you should always close any open handles in a finally clauseif (is != null)
{
try
{
is.close();
}
catch (IOException e)
{}
}
}
// try parse the string to a JSON objecttry
{
jObj = newJSONObject(json);
}
catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
catch (NullPointerException e)
{
Log.e("JSON Parser", "json String was null");
}
// return JSON Stringreturn jObj;
}
publicstatic JSONObject getJSONFromUrlWithParams(String url, List<NameValuePair> params)
{
InputStreamis=null;
Stringjson=null;
JSONObjectjObj=null;
// Making HTTP requesttry
{
// defaultHttpClientDefaultHttpClienthttpClient=newDefaultHttpClient();
StringparamString= URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGethttpGet=newHttpGet(url);
Log.d("url", url);
HttpResponsehttpResponse= httpClient.execute(httpGet);
HttpEntityhttpEntity= httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedReaderreader=newBufferedReader(newInputStreamReader(is, "iso-8859-1"), 20);
StringBuildersb=newStringBuilder();
Stringline=null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
json = sb.toString();
Log.d("JSON", json);
}
catch (Exception e)
{
Log.e("Buffer Error", "Error converting result " + e.toString());
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// try parse the string to a JSON objecttry
{
jObj = newJSONObject(json);
}
catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Stringreturn jObj;
}
}
Note that this is just a possible implementation that developed slowly as the OP supplied more code. The answer by JayR is an probably the better fix. The choice to make the JSONParser have static methods is an optional design decision. However, the changes made to closing the InputStream safely probably should still be applied.
Solution 2:
This is because you only use a single instance of JSONParser when you are calling a web service call.
Try to create a new instance for every call so that they have different response.
you can do such.
JSONParser jParser = newJSONParser();
JSONObject json = jParser.getJSONFromUrl(JOB_URL);
'json' has now the value of response you want.
Hope it helps. Cheers!
Post a Comment for "Json Keep Getting The Old Data"