我正在尝试从服务器解析一些数据并将数据与数据库中的数据进行比较......一切似乎都很好......除了即使比较数据发生变化我也只得到相同的值......我会用编码...
在下面的代码中,有 textviews tv3 和 tv4 ......即使比较数据,即 shopid 改变了,它也显示了来自 json 的第一个数组的值......
当我尝试调试时,我发现 runOnUithread 中的 run() 在 2-3 个循环后调用...我不知道这里发生了什么...请帮助解决问题...
编辑:在调试时,当我跳到每一行时,它没有显示任何错误...但是当我尝试使用 F9 跳到断点时,它显示错误...
编辑:2:由于代码有点长,我只是删除了一些不需要的部分......
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
urlConnection.disconnect();
Log.w("ImageDownloader", "Error downloading image from " + url);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
private class GetJsondata extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(getActivity(), "", getActivity().getResources().getString(R.string.pleasewait), true);
}
@Override
protected String doInBackground(Void... arg0) {
// Creating service handler class instance
ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
jsonObj = new JSONObject(jsonStr);
ObjectOutput out = new ObjectOutputStream(new FileOutputStream
(new File(getActivity().getCacheDir(), "") + File.separator + "cacheFile.srl"));
out.writeObject(jsonObj.toString());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} else {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream
(new File(getActivity().getCacheDir() + File.separator + "cacheFile.srl")));
jsonObj = new JSONObject((String) in.readObject());
in.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (OptionalDataException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
if (jsonObj != null) {
try {
ofrList = new ArrayList<ArrayList<String>>();
// Getting JSON Array node
jsonArray = jsonObj.getJSONArray("offers");
shoplistarray = new ArrayList<ArrayList<String>>();
// looping through All Contacts
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
url1 = c.getString("url");
if (userPrefs.getString("locale", null) == null || userPrefs.getString("locale", null).equals("en")) {
desc = c.getString("desc");
} else {
desc = c.getString("desc_ar");
}
expdate = c.getString("expdate");
ofrdate = c.getString("date");
shopsarray = c.getJSONArray("shops");
for (int n = 0; n < shopsarray.length(); n++) {
JSONObject jobj = shopsarray.getJSONObject(n);
shopid = jobj.getString("shopid");
if (shopid.equals(id) && tv3.getText().toString().equals("") ) {
downloadBitmap(url1);
finalI = i;
getActivity().runOnUiThread(new Runnable() {
public void run() {
if (activeNetwork != null && activeNetwork.isConnected()) {
image.setImageBitmap(bitmap);
saveFileInCache(String.valueOf(finalI), bitmap);
} else {
getFileOutOfCache(String.valueOf(finalI));
image.setImageBitmap(bitmap);
}
tv3.setText(desc);
tv4.setText(getActivity().getResources().getString(R.string.validtill) + expdate);
}
});
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.urnotconnected), Toast.LENGTH_LONG).show();
}
});
}
return desc;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
private Bitmap getFileOutOfCache(String fileName) {
final String cachePath = getActivity().getCacheDir().getPath();
File myDiskCacheFilePath = new File(cachePath);
File myCachedFile = new File(myDiskCacheFilePath
+ File.separator + fileName);
if (myCachedFile.exists()) {
bitmap = BitmapFactory.decodeFile(myCachedFile.toString());
} else {
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.cachedfilenotexist), Toast.LENGTH_LONG).show();
}
return bitmap;
}
public void saveFileInCache(String aFileName, Bitmap myImage) {
final String cachePath = getActivity().getCacheDir().getPath();
File myDiscCacheFilePath;
myDiscCacheFilePath = new File(cachePath);
File myDiscCacheFile = new File(myDiscCacheFilePath + File.separator + aFileName);
try {
FileOutputStream out = new FileOutputStream(myDiscCacheFile);
myImage.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.cachefailed), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}