[英]Android handling HTTP response
我剛剛開始編寫我的第一個Android應用程序,但遇到了問題。 我不知道如何正確處理HTTP響應。 我找到了創建GET請求(使用okhttp)的方法,並找到了處理Callable響應的方法。 我正在獲取對象的數組,然后需要為RecycleView創建新的適配器(具有此數組),但是我無法從主線程設置適配器。 我不知道如何從該Callable獲取數據。
提出要求
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);
}
和征兆
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
也許我不需要在靜態環境中這樣做?
編寫如下的單個類,
MyTask類{
//創建回調接口
//編寫用於遠程操作的代碼
//遠程操作完成后,回調綁定活動
//不要忘記將您的活動與此類取消綁定,因為這將導致內存泄漏,並且當您的活動處於停止階段時,它將崩潰,因為在停止回調后您將無法更新UI,因此請刪除此調用停下來。
}
就像這樣,你會發現它有用
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) {
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.