简体   繁体   中英

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. Here is the new activity code

ProgressDialog dialog;

@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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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