将数据解析为数组

发布于 2024-12-15 08:37:09 字数 1824 浏览 0 评论 0原文

好吧,面对这个问题,我无法将 try{} 内部的数据解析到外部的数据。 我在这里要完成的目标是什么...

for 循环将运行并向数组(图像)添加一个字符串(img)。 实际上它会将某些内容解析到数组中..但我无法将其放在数组之外 try/catch 块。有人可以帮我解决这个问题吗?

亲切的问候!

        protected void onPostExecute(Void unused)
        {
                try
                {           

                     jArray = new JSONArray(result);
                     String[] image = new String[jArray.length()];
                     Log.v("Crash Detect", "onPostExecute -> try");
                     JSONObject json_data=null;

                     for(int i=0;i<jArray.length();i++)
                     {
                         Log.v("Crash Detect", "onPostExecute -> forloop");
                        json_data = jArray.getJSONObject(i);


                        merk=json_data.getString("printerMerk");
                        type=json_data.getString("printerType");
                        content = json_data.getString("content");
                        img = json_data.getString("Img");



                        Arrays.fill(image, img);


                    } 
                 }
                 catch(JSONException e1)
                 {
                     Toast.makeText(getBaseContext(), "Niks gevonden" ,Toast.LENGTH_LONG).show();
                 } 
                catch (ParseException e1) 
                {
                       e1.printStackTrace();
                }   



            dialog.dismiss();


            Button home = (Button) findViewById(R.id.home);
            Button printerType = (Button) findViewById(R.id.printerType);

            home.setText(merk);
            printerType.setText(type);


            list=(ListView)findViewById(R.id.list);
            adapter=new LazyAdapter(doc.this, image);
            list.setAdapter(adapter);
        }

Ok, facing the problem I can't parse my data from inside the try{} to something outside.
What is my goal to complete here...

The for-loop will run and add a string (img) to the array (image).
Actually it parses something to the array.. but I can't get it outside the
try/catch-block. Can somebody help me with this?

Kind regards!

        protected void onPostExecute(Void unused)
        {
                try
                {           

                     jArray = new JSONArray(result);
                     String[] image = new String[jArray.length()];
                     Log.v("Crash Detect", "onPostExecute -> try");
                     JSONObject json_data=null;

                     for(int i=0;i<jArray.length();i++)
                     {
                         Log.v("Crash Detect", "onPostExecute -> forloop");
                        json_data = jArray.getJSONObject(i);


                        merk=json_data.getString("printerMerk");
                        type=json_data.getString("printerType");
                        content = json_data.getString("content");
                        img = json_data.getString("Img");



                        Arrays.fill(image, img);


                    } 
                 }
                 catch(JSONException e1)
                 {
                     Toast.makeText(getBaseContext(), "Niks gevonden" ,Toast.LENGTH_LONG).show();
                 } 
                catch (ParseException e1) 
                {
                       e1.printStackTrace();
                }   



            dialog.dismiss();


            Button home = (Button) findViewById(R.id.home);
            Button printerType = (Button) findViewById(R.id.printerType);

            home.setText(merk);
            printerType.setText(type);


            list=(ListView)findViewById(R.id.list);
            adapter=new LazyAdapter(doc.this, image);
            list.setAdapter(adapter);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2024-12-22 08:37:09

在 try/catch 之外声明该变量,然后在要使用它时检查它是否已设置。

  protected void onPostExecute(Void unused)
  {
       String[] image = null;
        try
        {           
            jArray = new JSONArray(result);
            image = new String[jArray.length()];
            // extra code

        }
        catch(JSONException e1)
        {
            Toast.makeText(getBaseContext(), "Niks gevonden" ,Toast.LENGTH_LONG).show();
        } 
        catch (ParseException e1) 
        {
            e1.printStackTrace();
        }   
        // perform extra code

        // NOTE: be sure to check image before using it as it might be null if exception is thrown!
        if(image != null)
        {
            // use image 
            adapter=new LazyAdapter(doc.this, image);
        }

Declare the variable outside the try/catch and then check if it is set when you want to use it.

  protected void onPostExecute(Void unused)
  {
       String[] image = null;
        try
        {           
            jArray = new JSONArray(result);
            image = new String[jArray.length()];
            // extra code

        }
        catch(JSONException e1)
        {
            Toast.makeText(getBaseContext(), "Niks gevonden" ,Toast.LENGTH_LONG).show();
        } 
        catch (ParseException e1) 
        {
            e1.printStackTrace();
        }   
        // perform extra code

        // NOTE: be sure to check image before using it as it might be null if exception is thrown!
        if(image != null)
        {
            // use image 
            adapter=new LazyAdapter(doc.this, image);
        }
雨夜星沙 2024-12-22 08:37:09

在 try catch 外部声明变量并将其设置在 try catch 内部。

然后你就可以在 try catch 之外访问它。

例如:

  protected void onPostExecute(Void unused) 
    {
JSONArray jArray;
                String[] image;
        JSONObject json_data=null;



        try
        {           
jArray = new JSONArray(result);
image = new String[jArray.length()];


        Log.v("Crash Detect", "onPostExecute -> try");

        for(int i=0;i<jArray.length();i++)
        {
            Log.v("Crash Detect", "onPostExecute -> forloop");
            json_data = jArray.getJSONObject(i);


            merk=json_data.getString("printerMerk");
            type=json_data.getString("printerType");
            content = json_data.getString("content");
            img = json_data.getString("Img");



            Arrays.fill(image, img);


        } 
    }
    catch(JSONException e1)
    {
        Toast.makeText(getBaseContext(), "Niks gevonden" ,Toast.LENGTH_LONG).show();
    } 
    catch (ParseException e1) 
    {
        e1.printStackTrace();
    }   



    dialog.dismiss();


    Button home = (Button) findViewById(R.id.home);
    Button printerType = (Button) findViewById(R.id.printerType);

    home.setText(merk);
    printerType.setText(type);


    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(doc.this, image);
    list.setAdapter(adapter);
}

Declare your variable outside the try catch and set it inside the try catch.

Then you would be able to access it outside the try catch.

For instance:

  protected void onPostExecute(Void unused) 
    {
JSONArray jArray;
                String[] image;
        JSONObject json_data=null;



        try
        {           
jArray = new JSONArray(result);
image = new String[jArray.length()];


        Log.v("Crash Detect", "onPostExecute -> try");

        for(int i=0;i<jArray.length();i++)
        {
            Log.v("Crash Detect", "onPostExecute -> forloop");
            json_data = jArray.getJSONObject(i);


            merk=json_data.getString("printerMerk");
            type=json_data.getString("printerType");
            content = json_data.getString("content");
            img = json_data.getString("Img");



            Arrays.fill(image, img);


        } 
    }
    catch(JSONException e1)
    {
        Toast.makeText(getBaseContext(), "Niks gevonden" ,Toast.LENGTH_LONG).show();
    } 
    catch (ParseException e1) 
    {
        e1.printStackTrace();
    }   



    dialog.dismiss();


    Button home = (Button) findViewById(R.id.home);
    Button printerType = (Button) findViewById(R.id.printerType);

    home.setText(merk);
    printerType.setText(type);


    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(doc.this, image);
    list.setAdapter(adapter);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文