2011-11-15 64 views
2

好的,面對問題,我無法從try {}內部解析數據到外部。 我的目標是在這裏完成...將數據解析到數組

for循環將運行並添加一個字符串(img)到數組(圖像)。 其實它解析的東西陣列..但我不能得到它在 try/catch-block之外。有人可以幫助我嗎?

親切的問候!

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

回答

1

聲明變量的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); 
     } 
+0

太棒了! 4moretogo .. – Jordy

0

在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); 
} 
+0

您不能定義jArray = new JSONArray(result);沒有嘗試/抓住它周圍的塊。 – Jordy

+0

你可以這樣,我現在擁有它。我不是故意把它放在try catch之外。 – prolink007