简体   繁体   English

异步json解析-我在做什么错?

[英]Async json parsing- What am i doing wrong?

I have been trying to work on an app in which after clicking ,a new activity opens up and loads the data from the url. 我一直在尝试在一个应用程序上工作,在该应用程序中,单击之后,将打开一个新活动并从URL加载数据。 Here is the new activity code 这是新的活动代码

ProgressDialog dialog; ProgressDialog对话框;

@Override

protected void onCreate(Bundle savedInstanceState) 
{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main2);

    }

    private class MyTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        dialog.setMessage("Processing");
        dialog.setIndeterminate(true);
        dialog.show();
        dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            JSONObject jsonObject = new JSONObject();
            String url = " http://www.trailermag.com/tsappapis/?request=featuredAdList";
            JSONArray trailersJSON = jsonObject.getJSONArray(url);
            for (int i = 0; i < trailersJSON.length(); i++) {
                Trail aTrail = new Trail();
                JSONObject contactObject = trailersJSON.getJSONObject(i);
                aTrail.id = contactObject.getString(V_Id);
                aTrail.image = contactObject.getString(V_Image);
                aTrail.title = contactObject.getString(V_Title);
                aTrail.price = contactObject.getString(V_Price);
                webData.add(aTrail);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (dialog.isShowing()) {
            System.out.println("IN POST EXE");
            dialog.dismiss();
        }

    }
}

Once try and replace this code with your code this will work and i have tested it. 一旦尝试用您的代码替换此代码,它将起作用,并且我已经对其进行了测试。

JSONArray mJsonArray = new JSONArray(response);
                for (int i = 0; i < mJsonArray.length(); i++) {
                    JSONObject mJsonObject = mJsonArray.getJSONObject(i);
                    String idStr = mJsonObject.getString("id");
                    String imageStr = mJsonObject.getString("image");
                    String titleStr = mJsonObject.getString("title");
                    String priceStr = mJsonObject.getString("price");
                }

Happeee...Programming.... Happeee ...正在编程...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM