2016-09-15 112 views
0

我剛剛開始編寫我的第一個Android應用程序,並面臨一個問題。我不知道如何正確處理HTTP響應。我已經找到了創建GET請求(使用okhttp)的方法,並找到了處理Callable響應的方法。我得到了我的對象數組,然後需要爲RecycleView(使用此數組)創建新的適配器,但我無法從主線程設置適配器。我不知道如何從Callable獲取數據。Android處理HTTP響應

製作要求

public static void getDonerPoints() { 
    try { 

     OkHttpClient client = new OkHttpClient(); 
     Request request = new Request.Builder() 
       .url("http://192.168.2.64:9000/donerPoint/get?count=100&orderBy=rating&orderDirection=asc") 
       .build(); 

     client.newCall(request).enqueue(
       new Callback() { 
        @Override 
        public void onFailure(Call call, IOException e) { 
         Log.d("mLog", "request error"); 
        } 

        @Override 
        public void onResponse(Call call, Response response) throws IOException { 
         try { 
          final ArrayList<DonerPoint> points = new ArrayList<>(); 
          if (response.code() == HttpURLConnection.HTTP_OK) { 
           JSONArray jsonArray = new JSONArray(response.body().string()); 
           ObjectMapper mapper = new ObjectMapper(); 
           for (int i = 0; i < jsonArray.length(); i++) 
            points.add(mapper.readValue(jsonArray.getString(i), DonerPoint.class)); 

           ListFragment.setNewContentForAdapter(points); 
           Log.d("mLog", "ok"); 
          } 
         } catch (Exception exc) { 
          Log.d("mLog", exc.toString()); 
         } 
        } 
       }); 

    } catch (Exception exc) { 
    } 
} 

設置適配器

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.list_tab, container, false); 
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.rv); 

    mLayoutManager = new LinearLayoutManager(getContext()); 
    mRecyclerView.setLayoutManager(mLayoutManager); 
    return rootView; 
} 

public static void setNewContentForAdapter(List<DonerPoint> points){ 
    mAdapter=new PointAdapter(points); 
    mRecyclerView.setAdapter(mAdapter); 
} 

而且Excexption

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 

也許我需要做的是不能在靜態情況下?

+0

請張貼一些代碼來說明你有什麼至今。 –

回答

0

寫入單個類像下面,

類MyTask {

//回調接口創建用於遠程操作

//寫入代碼

//一旦遠程操作來完成給回調到你的綁定活動

//不要忘記解開你的活動從這個類,因爲它會導致內存泄漏,因爲我們當你的活動停在停止階段時,它會崩潰,因爲在停止回叫之後你將不允許更新UI,所以在停止時刪除這個回調。

}

0

就是這樣的,你也會發現它的工作

public static void getDonerPoints() { 
    try { 

     OkHttpClient client = new OkHttpClient(); 
     Request request = new Request.Builder() 
       .url("http://192.168.2.64:9000/donerPoint/get?count=100&orderBy=rating&orderDirection=asc") 
       .build(); 

     client.newCall(request).enqueue(
       new Callback() { 
        @Override 
        public void onFailure(Call call, IOException e) { 
         Log.d("mLog", "request error"); 
        } 

        @Override 
        public void onResponse(Call call, Response response) throws IOException { 
         try { 
          new android.os.Handler(Looper.getMainLooper()).post(new Runnable() { 
          @Override 
           public void run() { 
           final ArrayList<DonerPoint> points = new ArrayList<>(); 
           if (response.code() == HttpURLConnection.HTTP_OK) { 
           JSONArray jsonArray = new JSONArray(response.body().string()); 
           ObjectMapper mapper = new ObjectMapper(); 
           for (int i = 0; i < jsonArray.length(); i++) 
            points.add(mapper.readValue(jsonArray.getString(i), DonerPoint.class)); 

           ListFragment.setNewContentForAdapter(points); 
           Log.d("mLog", "ok"); 
          } 
         } 
       }); 
         } catch (Exception exc) { 
          Log.d("mLog", exc.toString()); 
         } 
        } 
       }); 

    } catch (Exception exc) { 
    } 
}