0

私は次のjsonパーサーを持っています。

    public class JSONParserThreaded extends AsyncTask<String, Integer, String>
{
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    JSONObject processedjObj =null;

    JSONParserThreaded()
    {

    }

    @Override
    protected String doInBackground(String... params) 
    {
        // TODO Auto-generated method stub
        String url = params[0];
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent(); 
        }
        catch (UnsupportedEncodingException e) 
        {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } catch (IOException e) 
        {
            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);
            setJSONObject(jObj);

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

        // return JSON String

        return "Sucess";
    }

    void setJSONObject(JSONObject jObject)
    {
        processedjObj = jObject;
    }

    JSONObject returnJSONObject()
    {
        return processedjObj;
    }

    @Override
    protected void onPostExecute(String result) 
    {
        super.onPostExecute(result);

    }
}

次のコードでパーサーを使用しています

    public class CategoryScreenActivity extends Activity
{
    private static String url = "http://www.network.com/store/getCategories.php";

    static final String TAG_CATEGORY = "categories";
    static final String TAG_CATEGORY_NAME = "name";
    static final String TAG_CATEGORY_ID = "id";
    static final String TAG_CATEGORY_THUMBNAIL_URL = "thumbnail";
    static final String TAG_CATEGORY_IMAGE_MEDIUM_URL = "image_medium";
    static final String TAG_CATEGORY_IMAGE_LARGE_URL = "image_large";

    JSONArray categories = null;
    JSONObject wholeCategory = null;

    ListView mainListView;
    ListAdpater adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);

        ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();

        /*JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromURL(url);*/

        JSONParserThreaded jParserThread = new JSONParserThreaded();
        jParserThread.execute(url);

        try
        {
            //Getting Array of Categories
            wholeCategory = jParserThread.returnJSONObject();
            =>  categories = wholeCategory.getJSONArray(TAG_CATEGORY);  <=



            for(int i=0; i < categories.length(); i++)
            {
                JSONObject c = categories.getJSONObject(i);

                String cname = c.getString(TAG_CATEGORY_NAME);
                String cid = c.getString(TAG_CATEGORY_ID);
                String cThumbNailWithBackSlash = c.getString(TAG_CATEGORY_THUMBNAIL_URL);
                String cImageMediumWithBackSlash = c.getString(TAG_CATEGORY_IMAGE_MEDIUM_URL);
                String cImageLargeWithBackSLash = c.getString(TAG_CATEGORY_IMAGE_LARGE_URL);

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

                categoryMap.put(TAG_CATEGORY_ID,cid);
                categoryMap.put(TAG_CATEGORY_NAME, cname);
                categoryMap.put(TAG_CATEGORY_THUMBNAIL_URL, cThumbNailWithBackSlash);
                categoryMap.put(TAG_CATEGORY_IMAGE_MEDIUM_URL,cImageMediumWithBackSlash);
                categoryMap.put(TAG_CATEGORY_IMAGE_LARGE_URL,cImageLargeWithBackSLash);

                categoryList.add(categoryMap);
            }
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }
        mainListView = (ListView)findViewById(R.id.list);
        adapter = new ListAdpater(this, categoryList);
        mainListView.setAdapter(adapter);
    }


}

"=> <="で囲まれた行からnullポインター例外がスローされています 。アプリはデバッグモードでは正常に動作しますが、実行すると動作しません。誰か助けてくれませんか?

以下にスタックトレースを追加しました。

ここに画像の説明を入力してください

4

1 に答える 1

1

ネットワークをAsyncTaskまたはに委任する必要がありますIntentService。UI スレッドでネットワーキングを行うのは間違っています。

その件についてはこちらの記事をお読みください。そして、ここにAsyncTask チュートリアルがあります

于 2012-10-24T22:34:48.557 に答える