如何使用参数解析url中的JSON

时间:2014-02-23 16:48:20

标签: java android json url

这是我的AsyncTask:

public class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog = new ProgressDialog(GetData.this);
    protected void onPreExecute() {
        dialog.setMessage("Searching Database");
        dialog.show();
    }
    protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();
        // get JSON data from URL
        JSONArray json = jParser.getJSONFromUrl(url);


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

            try {
                JSONObject c = json.getJSONObject(i);
                String Listing_ID = c.getString(lblListing_ID);
                String Event_ID = c.getString(lblEvent_ID);
                String Venue_ID = c.getString(lblVenue_ID);
                String Start_Date = c.getString(lblStart_Date);
                String End_Date = c.getString(lblEnd_Date);
                String Frequency = c.getString(lblFrequency);

                HashMap<String, String> map = new HashMap<String, String>();

                // Add child node to HashMap key & value
                map.put(lblListing_ID, Listing_ID);
                map.put(lblEvent_ID, Event_ID);
                map.put(lblVenue_ID, Venue_ID);
                map.put(lblStart_Date, Start_Date);
                map.put(lblEnd_Date, End_Date);
                map.put(lblFrequency, Frequency);
                jsonlist.add(map);
             }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        ListAdapter adapter = new SimpleAdapter(GetData.this, jsonlist,
                R.layout.list_item, new String[] { lblListing_ID , lblEvent_ID,
                lblVenue_ID, lblStart_Date, lblEnd_Date, lblFrequency }, new int[] {
                R.id.Listing_ID, R.id.Event_ID, R.id.Venue_ID,
                R.id.Start_Date, R.id.End_Date, R.id.Frequency });
            setListAdapter(adapter);
        // select single ListView item
        lv = getListView();
    }
}

这是我的JSONParser类:

public class JSONParser {

static InputStream iStream = null;
static JSONArray jarray = null;
static String json = "";

public JSONParser() {
}

public JSONArray getJSONFromUrl(String url) {

    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e("==>", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    // Parse String to JSON object
    try {
        JSONObject object = new JSONObject( builder.toString());
        jarray = object.getJSONArray("listings");

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

    // return JSON Object
    return jarray;

}

}

当网址标准为:

http://192.168.1.74/android_connect/get_venues.php 

工作正常。但是,如果我添加如下参数:

http://192.168.1.74/android_connect/get_venues.php?Venue_Name=Venue Name 

它给出错误说活动已泄露窗口。这是logcat:

23915-23915/com.familiestvw.whatson E/WindowManager﹕ Activity com.familiestvw.whatson.GetData has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42f9a980 V.E..... R......D 0,0-1026,288} that was originally added here
android.view.WindowLeaked: Activity com.familiestvw.whatson.GetData has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42f9a980 V.E..... R......D 0,0-1026,288} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:450)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
        at android.app.Dialog.show(Dialog.java:287)
        at com.familiestvw.whatson.GetData$ProgressTask.onPreExecute(GetData.java:84)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at com.familiestvw.whatson.GetData.onCreate(GetData.java:77)
        at android.app.Activity.performCreate(Activity.java:5372)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)

关于问题是什么以及如何解决问题的任何想法都将非常感激。

1 个答案:

答案 0 :(得分:0)

AsyncTask中可能存在错误,导致Activity关闭,然后当您尝试打开dialog时,会发生异常。我建议检查早期的日志,也尝试打印从URL获得的内容。