java 响应主线程_java – Android,Volley Request,响应是阻塞主线程

使用Volley处理大量响应时会发生一些不好的事情:

String url = AppHelper.DOMAIN + "/service/pages/profile_update.json";

this.infoTextView.setText(getString(R.string.profile_info_updating));

final StringRequest stringRequest = new StringRequest(Request.Method.POST, url,

new Response.Listener() {

@Override

public void onResponse(String response) {

try {

JSONObject json = new JSONObject(response);

if (json.getBoolean("success")) {

// manage JSON object here

} else {

Toast.makeText(ProfileActivity.this,

getString(R.string.connection_problem_server),

Toast.LENGTH_LONG).show();

}

} catch (JSONException e) {

ProfileActivity.this.infoTextView.setText(

getString(R.string.profile_info_updating_error));

e.printStackTrace();

}

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

ProfileActivity.this.infoTextView.setText(

getString(R.string.profile_info_updating_error));

if (error.networkResponse != null && error.networkResponse.statusCode == 401) {

Toast.makeText(ProfileActivity.this,

getString(R.string.connection_problem_permission),

Toast.LENGTH_LONG).show();

}

new android.os.Handler().postDelayed(new Runnable() {

@Override

public void run() {

if (ProfileActivity.this.swipeRefreshLayout != null) {

ProfileActivity.this.swipeRefreshLayout.setRefreshing(false);

}

}

}, 1000);

error.printStackTrace();

}

}) {

@Override

protected Map getParams() {

Map params = new HashMap<>();

params.put("auth_token", ProfileActivity.this.defaultUser.getAuthenticationToken());

return params;

}

@Override

public Map getHeaders() throws AuthFailureError {

Map params = new HashMap<>();

params.putAll(super.getHeaders());

params.put("Accept-Encoding", "gzip,deflate");

return params;

}

@Override

protected Response parseNetworkResponse(NetworkResponse response) {

StringBuilder output = new StringBuilder();

try {

GZIPInputStream gStream = new GZIPInputStream(new ByteArrayInputStream(response.data));

InputStreamReader reader = new InputStreamReader(gStream);

BufferedReader in = new BufferedReader(reader, 16384);

String read;

while ((read = in.readLine()) != null) {

output.append(read).append("\n");

}

reader.close();

in.close();

gStream.close();

} catch (IOException error) {

error.printStackTrace();

return Response.error(new ParseError());

}

return Response.success(output.toString(), HttpHeaderParser.parseCacheHeaders(response));

}

};

stringRequest.setRetryPolicy(new RetryPolicy() {

@Override

public int getCurrentTimeout() {

// 40 seconds

return 40000;

}

@Override

public int getCurrentRetryCount() {

return DefaultRetryPolicy.DEFAULT_MAX_RETRIES;

}

@Override

public void retry(VolleyError error) throws VolleyError {

throw error;

}

});

Volley.newRequestQueue(this).add(stringRequest);

这段代码阻塞了主线程,冻结了应用程序.

另外,我设置了一些标头值以允许gzip响应和代码来处理数据.但这不是造成不良行为的因素.仅当onResponse(String response)启动时,应用程序才会冻结.

我该怎么做才能避免这种情况?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值