JSON解析数据需要重新启动应用程序

用户

对不起,我的英语不好...这是数据视图活动的一部分

DataView活动:

    /** 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 db中,并显示在名为favourite items activity..的列表视图中。问题是,当我在喜欢的项目中添加一些数据时,它被添加了。成功地。但是,当我单击列表视图中的项目以获取项目的详细信息时,它没有显示出来,它给出了错误。但是,当我重新启动我的应用程序并单击列表视图中的项目时,它工作正常..每次当我添加一些项目详细信息时,都不会显示,但是当我重新启动时,它会正常工作..

用户

调试后我得到了解决方案,问题是的价值,Mainactivity.a并且Favourite.a是重叠的。因此responce为null。但最后我的变化值favourite.a = 1,以MainActvity.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();
        }

现在它的工作正常了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JPA同步问题,需要重新启动应用程序

来自分类Dev

重新启动应用程序后,SQLite 数据消失了

来自分类Dev

为什么在编译数据库包之后每次都需要重新启动Java应用程序?

来自分类Dev

新安装的应用程序需要重新启动才能显示在启动菜单中

来自分类Dev

/etc/resolve.conf的更新需要重新启动应用程序

来自分类Dev

需要重新启动应用程序才能显示更改吗?

来自分类Dev

当用户接受 READ_EXTERNAL_STORAGE 权限时需要重新启动应用程序

来自分类Dev

重新启动应用程序时,GC日志循环数据丢失

来自分类Dev

每次重新启动应用程序时,NSUserDefaults中的数据均消失

来自分类Dev

每次重新启动应用程序时都会丢失数据

来自分类Dev

Android-Dropbox数据存储区为“空”,直到应用程序重新启动

来自分类Dev

重新启动应用程序后从SharedPreferences恢复时,Set <String>丢失数据

来自分类Dev

重新启动应用程序后,SQLite中未保留数据

来自分类Dev

重新启动应用程序后,为什么我的数据没有保存到CoreData?

来自分类Dev

重新启动应用程序后,SQLite丢失数据(插入,更新)

来自分类Dev

应用程序重新启动之间无延迟的持久数据

来自分类Dev

重新启动应用程序时如何删除SQLite数据库?

来自分类Dev

每次重新启动应用程序时,SQLite 数据库都会被删除

来自分类Dev

重新启动我的应用程序时,firebase 替换了我的数据

来自分类Dev

CLLocationManager startMonitoringForRegion:设备重新启动后不重新启动应用程序

来自分类Dev

从终端重新启动bash而不重新启动终端应用程序(mac)?

来自分类Dev

如何在Qt中重新启动应用程序?

来自分类Dev

以编程方式重新启动应用程序?

来自分类Dev

用singleTop重新启动MainActivity或应用程序。

来自分类Dev

单击通知时重新启动应用程序

来自分类Dev

崩溃后如何重新启动Qt应用程序?

来自分类Dev

ServiceStack自托管应用程序重新启动

来自分类Dev

如何调试应用程序重新启动的原因

来自分类Dev

重新启动应用程序后导航栏缩小

Related 相关文章

  1. 1

    JPA同步问题,需要重新启动应用程序

  2. 2

    重新启动应用程序后,SQLite 数据消失了

  3. 3

    为什么在编译数据库包之后每次都需要重新启动Java应用程序?

  4. 4

    新安装的应用程序需要重新启动才能显示在启动菜单中

  5. 5

    /etc/resolve.conf的更新需要重新启动应用程序

  6. 6

    需要重新启动应用程序才能显示更改吗?

  7. 7

    当用户接受 READ_EXTERNAL_STORAGE 权限时需要重新启动应用程序

  8. 8

    重新启动应用程序时,GC日志循环数据丢失

  9. 9

    每次重新启动应用程序时,NSUserDefaults中的数据均消失

  10. 10

    每次重新启动应用程序时都会丢失数据

  11. 11

    Android-Dropbox数据存储区为“空”,直到应用程序重新启动

  12. 12

    重新启动应用程序后从SharedPreferences恢复时,Set <String>丢失数据

  13. 13

    重新启动应用程序后,SQLite中未保留数据

  14. 14

    重新启动应用程序后,为什么我的数据没有保存到CoreData?

  15. 15

    重新启动应用程序后,SQLite丢失数据(插入,更新)

  16. 16

    应用程序重新启动之间无延迟的持久数据

  17. 17

    重新启动应用程序时如何删除SQLite数据库?

  18. 18

    每次重新启动应用程序时,SQLite 数据库都会被删除

  19. 19

    重新启动我的应用程序时,firebase 替换了我的数据

  20. 20

    CLLocationManager startMonitoringForRegion:设备重新启动后不重新启动应用程序

  21. 21

    从终端重新启动bash而不重新启动终端应用程序(mac)?

  22. 22

    如何在Qt中重新启动应用程序?

  23. 23

    以编程方式重新启动应用程序?

  24. 24

    用singleTop重新启动MainActivity或应用程序。

  25. 25

    单击通知时重新启动应用程序

  26. 26

    崩溃后如何重新启动Qt应用程序?

  27. 27

    ServiceStack自托管应用程序重新启动

  28. 28

    如何调试应用程序重新启动的原因

  29. 29

    重新启动应用程序后导航栏缩小

热门标签

归档