2017-03-16 7 views
0

私はVolleyのリクエストを受け取りましたが、変数に結果セットを取得しようとしていますが、「変数 'stuff'が内部クラスからアクセスされていることを宣言する必要があります最後の。"問題は、一度最終的に宣言すると、(tryブロックにしようとしているように)変数をレスポンスと同じにすることができないということです。私はこれについて議論する他の質問を見回しましたが、本当に私の状況に役立つものはありません。 コードはAndroid Volleyから変数にデータを設定

public ArrayList getStuff() { 
    ArrayList<JSONObject> stuff; 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // Instantiate the RequestQueue. 
      RequestQueue queue =  MySingleton.getInstance(getApplicationContext()).getRequestQueue(); 
      String url ="http://localhost:6000/stuff"; 

      RequestFuture<String> future = RequestFuture.newFuture(); 
      // Testing out blocking 
      StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future); 
      // Add the request to the RequestQueue. 
      queue.add(stringRequest); 

      try { 
       String response = future.get(30, TimeUnit.SECONDS); 
       stuff = response; 
       Log.i("tutors after blocked: ", tutors); 
      } catch(InterruptedException e) { 
       e.printStackTrace(); 
      } catch(ExecutionException e) { 
       e.printStackTrace(); 
      } catch (TimeoutException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

    // I want to be able to return 'stuff' here 
    return stuff; 

} 
+0

であることを意味しますまだデータを入手してください。 – xiaoyuan

+0

この 'final ArrayListを試してください stuff;' – ken

+0

申し訳ありませんが、これを言及すべきでしたが、最終として宣言すると、stuff = response(tryブロック内)を設定することはできません。それが私が直面していた問題です。私はそれを含めるために投稿を編集します。 – ProjectProgramAMark

答えて

0

次のとおりです。

public ArrayList getStuff() { 
final ArrayList<JSONObject> stuff; <--Add final here 
new Thread(new Runnable() { 
    @Override 
    public void run() { 
     // Instantiate the RequestQueue. 
     RequestQueue queue =  MySingleton.getInstance(getApplicationContext()).getRequestQueue(); 
     String url ="http://localhost:6000/stuff"; 

     RequestFuture<String> future = RequestFuture.newFuture(); 
     // Testing out blocking 
     StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future); 
     // Add the request to the RequestQueue. 
     queue.add(stringRequest); 

     try { 
      String response = future.get(30, TimeUnit.SECONDS); 
      stuff = response; 
      Log.i("tutors after blocked: ", tutors); 
     } catch(InterruptedException e) { 
      e.printStackTrace(); 
     } catch(ExecutionException e) { 
      e.printStackTrace(); 
     } catch (TimeoutException e) { 
      e.printStackTrace(); 
     } 
    } 
}).start(); 

// I want to be able to return 'stuff' here 
return stuff; 

}

+0

しかし、私は "最後の変数 'stuff'に値を代入することはできません" – ProjectProgramAMark

0

ちょうど方法外側その変数の宣言を移動します。これは、あなたがそれを返すとき、あなたがnullのArrayList returned.Causeを得ますfinal.But、新しいスレッドがまだ実行してdid'tされるとしてそれを宣言する必要があり、すでにそのクラスのグローバル変数

ArrayList<JSONObject> stuff; 

public ArrayList getStuff() { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      // Instantiate the RequestQueue. 
      RequestQueue queue =  MySingleton.getInstance(getApplicationContext()).getRequestQueue(); 
      String url ="http://localhost:6000/stuff"; 

      RequestFuture<String> future = RequestFuture.newFuture(); 
      // Testing out blocking 
      StringRequest stringRequest = new StringRequest(Request.Method.GET, url, future, future); 
      // Add the request to the RequestQueue. 
      queue.add(stringRequest); 

      try { 
       String response = future.get(30, TimeUnit.SECONDS); 
       stuff = response; 
       Log.i("tutors after blocked: ", tutors); 
      } catch(InterruptedException e) { 
       e.printStackTrace(); 
      } catch(ExecutionException e) { 
       e.printStackTrace(); 
      } catch (TimeoutException e) { 
       e.printStackTrace(); 
      } 
     } 
    }).start(); 

    // I want to be able to return 'stuff' here 
    return stuff; 

} 
+0

しかし、 "最終的な変数 'stuff'に値を代入できません" – ProjectProgramAMark

+0

public voidの直後にrunnableに移動してみてくださいrun(){。 –

+0

それは目的を破る。私はこの変数を関数の残りの部分にアクセスできるようにしようとしています。getStuff()@ Mr.Ed – ProjectProgramAMark