2014-10-16 93 views
0

对不起,我的英文不好...这里是数据视图活动的某些部分JSON解析数据需要重新启动应用程序

数据视图活动:

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    /* 
    * StrictMode is most commonly used to catch accidental disk or network 
    * access on the application's main thread 
    */ 

    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectDiskReads().detectDiskWrites().detectNetwork() 
      .penaltyLog().build()); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view); 

    name = (TextView) findViewById(R.id.tv_name); 
    fb = (ImageButton) findViewById(R.id.btn_fb); 
    name1 = (TextView) findViewById(R.id.tv_name1); 
    favo = (ImageButton) findViewById(R.id.btn_fav); 
    contact = (TextView) findViewById(R.id.tv_contact); 
    address = (TextView) findViewById(R.id.tv_address); 
    category = (TextView) findViewById(R.id.tv_category); 
    view_map = (ImageButton) findViewById(R.id.view_direction); 

    db = new SQLController(this); 

    url = getResources().getString(R.string.url); 


    Bundle b = getIntent().getExtras(); 
    data = b.getString("itemName"); 

    new async_dataDetail(DataViewActivity.this).execute(); 

    favo.setOnClickListener(this); 
    view_map.setOnClickListener(this); 
    fb.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.btn_fav: 

     String myUser = name.getText().toString(); 
     String storedUser = db.Exist(myUser); 

     // If Username exist 

     if (myUser.equals(storedUser)) { 

      Toast.makeText(getApplicationContext(), "Already exists !!!", 
        Toast.LENGTH_SHORT).show(); 

     } else { 
      db.adddata(context, name.getText().toString(), category 
        .getText().toString(), id, MainActivity.a); 

      favoriteList = db.getFavList(); 

      Log.d("aaaa", name.getText().toString() 
        + category.getText().toString() + MainActivity.a); 
      Toast.makeText(getApplicationContext(), 
        "Successfully, added to Favourities", 0).show(); 
     } 
     break; 

    } 
} 

class async_dataDetail extends AsyncTask<String, Void, Boolean> { 

    public async_dataDetail(DataViewActivity activity) { 

     context = activity; 
    } 

    @Override 
    protected Boolean doInBackground(String... arg0) { 

     // declare parameters that are passed to PHP script i.e. the 

     ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 

     if (MainActivity.a == 1) { 

      postParameters.add(new BasicNameValuePair("777", data)); 

      try { 

       CustomHttpClient.executeHttpGet("777"); 
      } catch (Exception e1) { 

       e1.printStackTrace(); 
      } 

     } else if (Favourite.a == 1) { 
      postParameters.add(new BasicNameValuePair("524", data)); 

      try { 

       CustomHttpClient.executeHttpGet("524"); 
      } catch (Exception e1) { 

       e1.printStackTrace(); 
      } 
     } 

     String response = null; 

     // call executeHttpPost method passing necessary parameters 

     try { 

      response = CustomHttpClient.executeHttpPost(

      url, 

      postParameters); 

      // store the result returned by PHP script that runs 
      // MySQL query 

      String result = response.toString(); 

      // parse json data 
      try { 

       JSONArray jArray = new JSONArray(result); 

       for (int i = 0; i < jArray.length(); i++) { 

        JSONObject json_data = jArray.getJSONObject(i); 

        st_name = " " + json_data.getString("name"); 
        id = json_data.getString("id"); 
        st_name1 = " Name : " 
          + json_data.getString("name"); 

        st_contact = " Contact : " + " " 
          + json_data.getString("contact"); 
        st_category = " Category : " 
          + json_data.getString("Category"); 
        st_address = " Address : " 
          + json_data.getString("address"); 

        Log.d("favourite_data", st_name + " " + st_contact 
          + " " + st_category + " " + st_address); 

       } 
      } catch (JSONException e) { 
       Log.e("log_tag", "Error parsing data " + e.toString()); 
      } 

     } catch (Exception e) { 
      Log.e("log_tag", "Error in http connection!!" + e.toString()); 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 

     super.onPostExecute(result); 

     progressdialog.dismiss(); 

     try { 

      name.setText(st_name); 
      name1.setText(st_name1); 
      contact.setText(st_contact); 
      category.setText(st_category); 
      address.setText(st_address); 

     } catch (Exception e) { 

      Log.e("log_tag", "Error in Display!" + e.toString()); 

     } 

    } 

    @Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 

     progressdialog = new ProgressDialog(DataViewActivity.this); 
     progressdialog.setTitle("Processing...."); 
     progressdialog.setMessage("Please Wait....."); 
     progressdialog.setCancelable(false); 
     progressdialog.show(); 

    } 

} 

喜爱的物品的活动

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fav); 
    setTitle("Favorites"); 

    // listView = (ListView) findViewById(R.id.listView); 

    listView = getListView(); 
    db = new SQLController(this); 

    favoriteList = db.getFavList(); 
    listView.setAdapter(new ViewAdapter()); 

} 

public class ViewAdapter extends BaseAdapter { 

    LayoutInflater mInflater; 

    public ViewAdapter() { 
     mInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public int getCount() { 
     return favoriteList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 

     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.listitem, null); 
     } 

     nameText = (TextView) convertView.findViewById(R.id.nameText); 
     nameText.setText(" Name : " 
       + favoriteList.get(position).getName()); 
     final TextView ageText = (TextView) convertView 
       .findViewById(R.id.ageText); 
     ageText.setText(favoriteList.get(position).getAge()); 

     final Button delete = (Button) convertView 
       .findViewById(R.id.delete); 
     delete.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       db.removeFav(favoriteList.get(position).getId()); 

       favoriteList = db.getFavList(); 
       listView.setAdapter(new ViewAdapter()); 
      } 
     }); 

     convertView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       a = 1; 
       String aa = favoriteList.get(position).getCateg() 
         .toString(); 
       String bb = favoriteList.get(position).getTable() 
         .toString(); 
       intent.putExtra("itemName", aa + bb); 

       startActivity(intent); 

      } 
     }); 

     return convertView; 
    } 

} 

Logcat:

10-16 14:41:36.700: W/System.err(23063): at java.lang.Thread.run(Thread.java:864) 
10-16 14:41:36.830: D/OpenGLRenderer(23063): Flushing caches (mode 0) 
10-16 14:41:36.830: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x556ab000 size:6144000 offset:4608000 
10-16 14:41:36.830: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x58d7c000 size:7680000 offset:6144000 
10-16 14:41:36.830: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x54043000 size:4608000 offset:3072000 
10-16 14:41:37.831: E/log_tag(23063): Error parsing data org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray 
10-16 14:41:37.891: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x56291000 size:15728640 offset:15237120 
10-16 14:41:37.891: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x594cf000 size:22794240 offset:22302720 
10-16 14:41:37.901: D/memalloc(23063): /dev/pmem: Unmapping buffer base:0x5d32e000 size:24821760 offset:24330240 

我得到的一些数据在ViewData activity从通过JSON SQL数据库..本次活动当我按下按钮favo一些细节被添加到的SQLite数据库,并在名为favourite items activity列表视图中显示.. 问题是,当我在喜欢的项目中添加一些数据时,它会成功添加。但是当我点击列表视图中的项目以获取项目的详细信息时,它不会显示它会给出错误。但是当我重新启动我的应用程序,并在列表视图中单击项目,它工作正常..没有显示时,我添加一些项目细节每一次,但是当我重新启动它工作正常..

+0

错误本身告诉:'type org.json.JSONObject $ 1无法转换为JSONArray'。你正试图抓取'JSONArray'这意味着是'JSONObject' – 2014-10-16 10:38:52

+0

检查你在这里得到什么'JSONArray jArray = new JSONArray(result);'in ** result ** ??? – 2014-10-16 10:39:45

+0

得到同样的错误JSON解析.. – user 2014-10-16 10:54:07

回答

0

我调试完毕后得到解决,问题是值Mainactivity.aFavourite.a重叠。所以responce为空。但 最后我改变值favourite.a = 1MainActvity.a =2当收藏夹列表项被点击

if (MainActivity.a == 1) { 

     postParameters.add(new BasicNameValuePair("777", data)); 

     try { 

      CustomHttpClient.executeHttpGet("777"); 
     } catch (Exception e1) { 

      e1.printStackTrace(); 
     } 

    } else if (MainActivity.a == 2) { 
     postParameters.add(new BasicNameValuePair("524", data)); 

     try { 

      CustomHttpClient.executeHttpGet("524"); 
     } catch (Exception e1) { 

      e1.printStackTrace(); 
     } 

现在它的工作的罚款。

+0

它的工作原理但不是一个好的解决方案。 – Dev 2014-10-18 06:36:35

+0

y ..那么什么是正确的解决方案..你可以建议请... – user 2014-10-18 06:40:42

+0

什么是你的确切要求,解释! – Dev 2014-10-18 06:44:15