簡體   English   中英

異步json解析-我在做什么錯?

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

我一直在嘗試在一個應用程序上工作,在該應用程序中,單擊之后,將打開一個新活動並從URL加載數據。 這是新的活動代碼

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();
        }

    }
}

一旦嘗試用您的代碼替換此代碼,它將起作用,並且我已經對其進行了測試。

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 ...正在編程...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM