2016-09-15 94 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) { 
    } 
}