0

我正在尝试使用下面介绍的 JSONParser 类解析 JSON。

根据断点,这行代码似乎崩溃了:

HttpResponse httpResponse = httpClient.execute(httpPost);

我不知道该怎么做。我将不胜感激。

分段

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.agenda, container, false);
        final ListView listview = (ListView)view.findViewById(R.id.listView);
        String[] values = new String[] {"Summit 2013","Sponsors","Agenda","Survey","Website","Credits" };

        Log.v(TAG,"Created");
        String[] array = new String[10];
        array[0] = "";

        **new RetrieveFeedTask().doInBackground("");**

异步任务

class RetrieveFeedTask extends AsyncTask<String, Void, JSONObject> {

        private Exception exception;
        protected JSONObject doInBackground(String... params) {
            try {
               // URL url= new URL(urls[0]);
                JSONParser parser = new JSONParser();
                JSONObject jobject = parser.getJSONFromUrl("http://www.itenwired.com/json");
                return jobject;
            } catch (Exception e) {

                this.exception = e;
                Log.v(TAG,"Exception"+exception.getLocalizedMessage());
                return null;
            }
        }
        protected void onPostExecute(JSONObject result) {
            Log.v(TAG,"Downloaded");
            Log.v(TAG,result.toString());
        }
    }

JSON解析器

package com.iten.itenwired2013;

/**
 * Created by jmc72 on 7/10/13.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.client.HttpResponseException;

import android.util.Log;

public class JSONParser {
    private static final String TAG = "JSON PARSER";

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
           // Log.v(TAG, httpPost.getURI().toString());
           //new HttpPost();
            httpPost.setHeader("Content-type", "application/json");
            Log.v(TAG, "01");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            Log.v(TAG, "Aaa " + httpResponse.toString());
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
            Log.v(TAG, "1");

        }
        catch (HttpResponseException e) {
            Log.v(TAG, "000");
        }
        catch (UnsupportedEncodingException e) {
            Log.v(TAG, "2");
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            Log.v(TAG, "3");
            e.printStackTrace();
        } catch (IOException e) {
            Log.v(TAG, "4");
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());

        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

日志猫

07-12 14:18:19.713      322-656/system_process                 W/ActivityManager: Unbind failed: could not find connection for android.os.BinderProxy@40d233c0
07-12 14:18:20.953    1304-1304/?                              D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-12 14:18:20.953    1304-1304/?                              D/AndroidRuntime: CheckJNI is ON
07-12 14:18:21.002    1304-1304/?                              D/dalvikvm: Trying to load lib libjavacore.so 0x0
07-12 14:18:21.012    1304-1304/?                              D/dalvikvm: Added shared lib libjavacore.so 0x0
07-12 14:18:21.052    1304-1304/?                              D/dalvikvm: Trying to load lib libnativehelper.so 0x0
07-12 14:18:21.052    1304-1304/?                              D/dalvikvm: Added shared lib libnativehelper.so 0x0
07-12 14:18:21.993    1304-1304/?                              D/AndroidRuntime: Calling main entry com.android.commands.pm.Pm
07-12 14:18:22.072      322-345/system_process                 W/ActivityManager: No content provider found for permission revoke: file:///data/local/tmp/com.iten.itenwired2013
07-12 14:18:22.092      322-345/system_process                 W/ActivityManager: No content provider found for permission revoke: file:///data/local/tmp/com.iten.itenwired2013
07-12 14:18:22.103      322-345/system_process                 I/PackageManager: Copying native libraries to /data/app-lib/vmdl511006697
07-12 14:18:22.343      322-323/system_process                 D/dalvikvm: GC_CONCURRENT freed 603K, 58% free 5579K/13020K, paused 9ms+18ms, total 114ms
07-12 14:18:22.523      322-345/system_process                 I/PackageManager: Removing non-system package:com.iten.itenwired2013
07-12 14:18:22.523      322-342/system_process                 I/ActivityManager: Force stopping package com.iten.itenwired2013 appid=10046 user=-1
07-12 14:18:22.533      322-342/system_process                 I/ActivityManager: Killing proc 951:com.iten.itenwired2013/u0a10046: force stop com.iten.itenwired2013
07-12 14:18:22.533      322-342/system_process                 W/ActivityManager: Force removing ActivityRecord{40d17e90 u0 com.iten.itenwired2013/.MainActivity}: app died, no saved state
07-12 14:18:22.573      322-335/system_process                 I/WindowState: WIN DEATH: Window{416e8350 u0 com.iten.itenwired2013/com.iten.itenwired2013.MainActivity}
07-12 14:18:22.833       36-156/?                              E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
07-12 14:18:22.862      322-608/system_process                 W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 951 uid 10046
07-12 14:18:23.793      322-345/system_process                 I/PackageManager: Package com.iten.itenwired2013 codePath changed from /data/app/com.iten.itenwired2013-1.apk to /data/app/com.iten.itenwired2013-2.apk; Retaining data and using new
07-12 14:18:23.803      322-345/system_process                 I/PackageManager: Running dexopt on: com.iten.itenwired2013
07-12 14:18:25.242    1315-1315/?                              D/dalvikvm: DexOpt: load 271ms, verify+opt 815ms, 902996 bytes
07-12 14:18:25.272      322-342/system_process                 I/ActivityManager: Force stopping package com.iten.itenwired2013 appid=10046 user=-1
07-12 14:18:25.272      322-345/system_process                 W/PackageManager: Code path for pkg : com.iten.itenwired2013 changing from /data/app/com.iten.itenwired2013-1.apk to /data/app/com.iten.itenwired2013-2.apk
07-12 14:18:25.272      322-345/system_process                 W/PackageManager: Resource path for pkg : com.iten.itenwired2013 changing from /data/app/com.iten.itenwired2013-1.apk to /data/app/com.iten.itenwired2013-2.apk
07-12 14:18:25.402      322-345/system_process                 D/PackageManager: New package installed in /data/app/com.iten.itenwired2013-2.apk
07-12 14:18:25.643      322-323/system_process                 D/dalvikvm: GC_CONCURRENT freed 670K, 57% free 5603K/13020K, paused 8ms+15ms, total 125ms
07-12 14:18:25.653      322-345/system_process                 I/ActivityManager: Force stopping package com.iten.itenwired2013 appid=10046 user=0
07-12 14:18:25.763      662-662/com.android.launcher           D/dalvikvm: GC_EXPLICIT freed 1892K, 31% free 9913K/14264K, paused 5ms+10ms, total 91ms
07-12 14:18:25.933      322-411/system_process                 I/InputReader: Reconfiguring input devices.  changes=0x00000010
07-12 14:18:25.983      322-342/system_process                 I/ActivityManager: Start proc com.android.keychain for broadcast com.android.keychain/.KeyChainBroadcastReceiver: pid=1317 uid=1000 gids={41000, 1015, 1028, 3002, 3001, 3003}
07-12 14:18:26.083      723-844/com.android.contacts           W/AccountTypeManager: No authenticator found for type=com.android.exchange, ignoring it.
07-12 14:18:26.083      723-844/com.android.contacts           I/AccountTypeManager: Loaded meta-data for 0 account types, 0 accounts in 8ms(wall) 3ms(cpu)
07-12 14:18:26.153      613-613/android.process.acore          D/dalvikvm: GC_EXPLICIT freed 154K, 16% free 2804K/3336K, paused 4ms+5ms, total 354ms
07-12 14:18:26.172    1317-1317/com.android.keychain           E/Trace: error opening trace file: No such file or directory (2)
07-12 14:18:26.232    1317-1317/com.android.keychain           W/ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1352 android.content.ContextWrapper.startService:450 android.content.ContextWrapper.startService:450 com.android.keychain.KeyChainBroadcastReceiver.onReceive:12 android.app.ActivityThread.handleReceiver:2376
07-12 14:18:26.442      322-411/system_process                 I/InputReader: Reconfiguring input devices.  changes=0x00000010
07-12 14:18:26.763      322-402/system_process                 W/InputMethodManagerService: Found no subtypes in a system IME: com.android.inputmethod.pinyin
07-12 14:18:26.824      723-844/com.android.contacts           W/AccountTypeManager: No authenticator found for type=com.android.exchange, ignoring it.
07-12 14:18:26.824      723-844/com.android.contacts           I/AccountTypeManager: Loaded meta-data for 0 account types, 0 accounts in 6ms(wall) 2ms(cpu)
07-12 14:18:27.202      322-345/system_process                 D/dalvikvm: GC_EXPLICIT freed 481K, 58% free 5551K/13020K, paused 16ms+43ms, total 280ms
07-12 14:18:27.232    1304-1304/?                              D/AndroidRuntime: Shutting down VM
07-12 14:18:27.252    1304-1308/?                              D/dalvikvm: GC_CONCURRENT freed 94K, 18% free 467K/564K, paused 1ms+1ms, total 14ms
07-12 14:18:27.252    1304-1310/?                              D/jdwp: Got wake-up signal, bailing out of select
07-12 14:18:27.252    1304-1310/?                              D/dalvikvm: Debugger has detached; object registry had 1 entries
07-12 14:18:28.203      322-402/system_process                 W/RecognitionManagerService: no available voice recognition services found for user 0
07-12 14:18:28.213    1333-1333/?                              D/AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-12 14:18:28.213    1333-1333/?                              D/AndroidRuntime: CheckJNI is ON
07-12 14:18:28.322    1333-1333/?                              D/dalvikvm: Trying to load lib libjavacore.so 0x0
07-12 14:18:28.342    1333-1333/?                              D/dalvikvm: Added shared lib libjavacore.so 0x0
07-12 14:18:28.412    1333-1333/?                              D/dalvikvm: Trying to load lib libnativehelper.so 0x0
07-12 14:18:28.412    1333-1333/?                              D/dalvikvm: Added shared lib libnativehelper.so 0x0
07-12 14:18:29.773    1333-1333/?                              D/AndroidRuntime: Calling main entry com.android.commands.am.Am
07-12 14:18:29.783    1333-1333/?                              D/dalvikvm: Note: class Landroid/app/ActivityManagerNative; has 157 unimplemented (abstract) methods
07-12 14:18:29.813      322-571/system_process                 I/ActivityManager: Force stopping package com.iten.itenwired2013 appid=10046 user=-1
07-12 14:18:29.823      322-571/system_process                 I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.iten.itenwired2013/.MainActivity} from pid 1333
07-12 14:18:29.853      322-571/system_process                 W/WindowManager: Failure taking screenshot for (266x425) to layer 21005
07-12 14:18:29.894    1333-1333/?                              D/AndroidRuntime: Shutting down VM
07-12 14:18:29.913    1333-1337/?                              D/dalvikvm: GC_CONCURRENT freed 95K, 17% free 502K/600K, paused 1ms+1ms, total 20ms
07-12 14:18:29.913    1333-1339/?                              D/jdwp: Got wake-up signal, bailing out of select
07-12 14:18:29.913    1333-1339/?                              D/dalvikvm: Debugger has detached; object registry had 1 entries
07-12 14:18:29.933    1343-1343/com.iten.itenwired2013         D/dalvikvm: Not late-enabling CheckJNI (already on)
07-12 14:18:29.943      322-610/system_process                 I/ActivityManager: Start proc com.iten.itenwired2013 for activity com.iten.itenwired2013/.MainActivity: pid=1343 uid=10046 gids={50046, 3003, 1028}
07-12 14:18:30.353       36-156/?                              E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
07-12 14:18:30.542    1343-1343/com.iten.itenwired2013         E/Trace: error opening trace file: No such file or directory (2)
07-12 14:18:30.622    1343-1343/com.iten.itenwired2013         W/ActivityThread: Application com.iten.itenwired2013 is waiting for the debugger on port 8100...
07-12 14:18:30.763    1343-1343/com.iten.itenwired2013         I/System.out: Sending WAIT chunk
07-12 14:18:30.773    1343-1349/com.iten.itenwired2013         I/dalvikvm: Debugger is active
07-12 14:18:30.973    1343-1343/com.iten.itenwired2013         I/System.out: Debugger has connected
07-12 14:18:30.973    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:31.183    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:31.383    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:31.582    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:31.783    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:31.963      322-342/system_process                 I/Choreographer: Skipped 40 frames!  The application may be doing too much work on its main thread.
07-12 14:18:31.983    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:32.193    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:32.393    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:32.604    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:32.713      322-342/system_process                 I/Choreographer: Skipped 66 frames!  The application may be doing too much work on its main thread.
07-12 14:18:32.742       36-156/?                              E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
07-12 14:18:32.803    1343-1343/com.iten.itenwired2013         I/System.out: waiting for debugger to settle...
07-12 14:18:33.003    1343-1343/com.iten.itenwired2013         I/System.out: debugger has settled (1416)
07-12 14:18:33.393        36-67/?                              I/ARMAssembler: generated scanline__00000077:03515104_00008001_00000000 [113 ipp] (134 ins) at [0x40be3520:0x40be3738] in 1163000 ns
07-12 14:18:34.823    1343-1343/com.iten.itenwired2013         D/dalvikvm: GC_FOR_ALLOC freed 163K, 10% free 2530K/2808K, paused 40ms, total 57ms
07-12 14:18:34.834    1343-1343/com.iten.itenwired2013         I/dalvikvm-heap: Grow heap (frag case) to 3.185MB for 635812-byte allocation
07-12 14:18:34.903    1343-1352/com.iten.itenwired2013         D/dalvikvm: GC_FOR_ALLOC freed 2K, 9% free 3149K/3432K, paused 57ms, total 57ms
07-12 14:18:34.963    1343-1346/com.iten.itenwired2013         D/dalvikvm: GC_CONCURRENT freed <1K, 9% free 3149K/3432K, paused 12ms+4ms, total 62ms
07-12 14:18:34.963    1343-1343/com.iten.itenwired2013         D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 51ms
07-12 14:18:34.973    1343-1343/com.iten.itenwired2013         I/dalvikvm-heap: Grow heap (frag case) to 3.661MB for 500416-byte allocation
07-12 14:18:35.023    1343-1352/com.iten.itenwired2013         D/dalvikvm: GC_FOR_ALLOC freed <1K, 8% free 3637K/3924K, paused 50ms, total 50ms
07-12 14:18:36.323    1343-1343/com.iten.itenwired2013         V/AgendaActivity: Created
07-12 14:18:36.423    1343-1343/com.iten.itenwired2013         V/JSON PARSER: 01
07-12 14:18:39.882      322-339/system_process                 W/ActivityManager: Launch timeout has expired, giving up wake lock!
07-12 14:18:40.453      322-339/system_process                 W/ActivityManager: Activity idle timeout for ActivityRecord{40d1e810 u0 com.iten.itenwired2013/.MainActivity}
07-12 14:18:42.545    1343-1343/com.iten.itenwired2013         V/AgendaActivity: Exceptionnull
07-12 14:18:42.613    1343-1346/com.iten.itenwired2013         D/dalvikvm: GC_CONCURRENT freed 784K, 22% free 3245K/4144K, paused 6ms+24ms, total 102ms
07-12 14:18:42.862        36-36/?                              E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
07-12 14:18:43.363    1343-1343/com.iten.itenwired2013         I/Choreographer: Skipped 693 frames!  The application may be doing too much work on its main thread.
07-12 14:18:43.633    1343-1343/com.iten.itenwired2013         D/gralloc_goldfish: Emulator without GPU emulation detected.
07-12 14:18:43.882      322-341/system_process                 I/ActivityManager: Displayed com.iten.itenwired2013/.MainActivity: +14s4ms
07-12 14:18:44.333    1343-1343/com.iten.itenwired2013         I/Choreographer: Skipped 56 frames!  The application may be doing too much work on its main thread.
4

1 回答 1

0

你用得不好AsyncTask,千万不要打电话doInBackground,但execute("")

在这里解释:http: //developer.android.com/reference/android/os/AsyncTask.html

于 2013-07-12T15:08:35.753 回答