我正在使用从json
数据中检索值的代码。检查它并根据它在单listview
中制作自定义布局(3种不同布局)的值。
最初它工作正常。突然它会抛出错误“java.lang.String类型的值为1的NULL值无法转换为JSONObject”
变量plates
包含值,但仍显示NULL
HomeFragment.java
private class GetList_default extends AsyncTask<String, Void, Void> {
Dialog dialog = new Dialog(getActivity());
String jsonStr;
ArrayList<HashMap<String, String>> Local_newsList;
@Override
protected void onPreExecute() {
super.onPreExecute();
Local_newsList=new ArrayList<HashMap<String,String>>();
// Showing progress dialog
/*dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout._wait_dialog);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();*/
}
@Override
protected Void doInBackground(String... params) {
if (NetworkCheck.isNetworkAvailable(getActivity()) == true) {
//Log.d("index count for array", params[0]);
if(Integer.parseInt(params[0])==0){
//Log.d("index count for array","Rweached");
newsList = new ArrayList<HashMap<String, String>>();
}
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(All_link.HOME_DATA_URL + "/"+params[0]+"/"+ icount,
ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
news = jsonObj.getJSONArray(All_link.TAG_NEWS);
err = jsonObj.getString(All_link.TAG_ERROR);
String more = jsonObj.getString(All_link.TAG_MORE);
Log.e("------>Error",String.valueOf(err));
Log.e("------>More",String.valueOf(more));
if(more.equals("0")){
load_more = 0;
}else{
load_more = 1;
}
// looping through All Contacts
for (int i = 0; i < news.length(); i++) {
JSONObject segments_list = news.getJSONObject(i);
for (int plates_count = 0; plates_count < segments_list
.length(); plates_count++) {
String plates = "";
if (plates_count == 3) {
plates = String.valueOf("banner_image");
JSONObject segments_plates = segments_list
.getJSONObject(plates);
//String flag = "banner_image";
String id = segments_plates
.getString(All_link.TAG_BANNER_ID);
String banner_no = segments_plates
.getString(All_link.TAG_BANNER_NO);
String banner_image;
if(segments_plates.getString(All_link.TAG_BANNER_THUMB_URL)==""){
banner_image = All_link.TAG_NO_IMAGE;
}else{
banner_image = segments_plates.getString(All_link.TAG_BANNER_THUMB_URL);
}
String banner_status = segments_plates
.getString(All_link.TAG_BANNER_STATUS);
// tmp hashmap for single news
HashMap<String, String> news_hashmap = new HashMap<String, String>();
// adding each child node to HashMap key =>
// value
news_hashmap.put(All_link.TAG_BANNER_ID, id);
news_hashmap.put(All_link.TAG_BANNER_NO,
banner_no);
news_hashmap.put(All_link.TAG_BANNER_THUMB_URL,
banner_image);
news_hashmap.put(All_link.TAG_BANNER_STATUS,
banner_status);
/*news_hashmap
.put(All_link.TAG_BANNER_FLAG, flag);*/
// adding contact to contact list
newsList.add(news_hashmap);
addSeparatorItem();
} else {
plates = String.valueOf(plates_count + 1);
JSONObject segments_plates = segments_list.getJSONObject(plates);
if(segments_plates.getString(All_link.TAG_NEWS_TYPE).equals("2")){
//type_of_news = segments_plates.getString(All_link.TAG_NEWS_TYPE);
addSeparatorItem_for_live();
}
String id = segments_plates.getString(All_link.TAG_ID);
String news_title = segments_plates.getString(All_link.TAG_NEWS_TITLE);
String news_desc = segments_plates.getString(All_link.TAG_DESC);
String segment = segments_plates.getString(All_link.TAG_SEGMENT);
String plate = segments_plates.getString(All_link.TAG_PLATE);
String img ="";
if(segments_plates.getString(All_link.TAG_THUMB_URL).equals("")){
img = All_link.TAG_NO_IMAGE;
}else{
img = segments_plates.getString(All_link.TAG_THUMB_URL);
}
HashMap<String, String> news_hashmap = new HashMap<String, String>();
// adding each child node to HashMap key =>
// value
news_hashmap.put(All_link.TAG_ID, id);
news_hashmap.put(All_link.TAG_NEWS_TITLE, news_title);
news_hashmap.put(All_link.TAG_DESC, news_desc);
news_hashmap.put(All_link.TAG_SEGMENT, segment);
news_hashmap.put(All_link.TAG_PLATE, plate);
news_hashmap.put(All_link.TAG_THUMB_URL, img);
//news_hashmap.put(All_link.TAG_BANNER_FLAG, flag);
// adding contact to contact list
newsList.add(news_hashmap);
int val = 0;
int val2 = 1;
val = (Integer.parseInt(segment)%2)==0 ? val:val2;
if((Integer.parseInt(segment)%2)!=0){
Log.e("--->CHECKING ODD EVEN", String.valueOf(val));
addSeparatorItem();
addSeparatorItem_for_alternate();
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} else {
Log.e("Network Error", "Internet Connection Error");
error_flag = 1;
// error = "Internet Connection Error";
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(error_flag==1){
listView.setVisibility(8);
main_error.setText("Internet Connection Error! Please check your network settings and try again");
main_error.setVisibility(0);
img_error.setVisibility(0);
}else{
listView.setVisibility(0);
main_error.setText("");
main_error.setVisibility(8);
img_error.setVisibility(8);
int currentPosition = listView.getFirstVisiblePosition();
mAdapter = new MyCustomAdapter(getActivity(), newsList);
listView.setAdapter(mAdapter);
// Setting new scroll position
listView.setSelection(currentPosition);
//mAdapter.notifyDataSetChanged();
}
// Dismiss the progress dialog
if (dialog.isShowing())
dialog.dismiss();
SharedPreferences sharedPrefs = getActivity().getSharedPreferences(All_link.MyPREFERENCES,
Context.MODE_PRIVATE);
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
//For Breaking News Setting
if (sharedPrefs.getInt("prefswtch4", 1) == 1) {
if (All_link.GLOBAL_BREAKING_FLAG == 0) {
new getBreakingNews().execute();
}
}
//For Location Setting
if (sharedPrefs.getInt("prefswtch1", 1) == 1) {
} else {
}
//For Ticker Setting
if (sharedPrefs.getInt("prefswtch2", 1) == 1) {
new getTicker().execute();
} else {
marque.setVisibility(8);
}
}
}
答案 0 :(得分:1)
来自发布的代码:
plates = String.valueOf("banner_image");
JSONObject segments_plates = segments_list.getJSONObject(plates);
我非常确定&#34; banner_image&#34;不是有效的JSON对象,因此是错误。
(你可能也应该删除无用的String.valueOf(String)
)