向服务器发送数据时强制关闭
我是向服务器发送数据和使用 Volley 库的新手。
我使用以下代码使用启动屏幕中的 Volley 库将数据发送到服务器。
new Thread(new Runnable() {
@Override
public void run() {
List<Transaction> transactionsList = transactionDao.getAllTransactions();
for (int i = 0; i < transactionsList.size(); i++) {
Transaction transaction = transactionsList.get(i);
String mobile = partnersDao.selectPhoneNumberById(1);
String traderName = transaction.getTraderName();
String operation = transaction.getOperation();
String from = transaction.getFrom();
double amount = transaction.getAmount();
String reason = transaction.getReason();
long transactionDate = transaction.getTransactionDate();
long registrationDate = transaction.getRegistrationDate();
long time = transaction.getTime();
String transactionType = transaction.getTransactionType();
long transactionNum = transaction.getTransactionNum();
String description = transaction.getDescription();
int searchHelper = transaction.getSearchHelper();
StringRequest request = new StringRequest(Request.Method.POST, "http://myhost.ir/?command=set_transactions&mobile=" + mobile + "&trader_name=" + traderName + "&operation=" + operation + "&from=" + from + "&amount=" + amount + "&reason=" + reason + "&transaction_date=" + transactionDate + "®istration_date=" + registrationDate + "&time=" + time + "&transaction_type=" + transactionType + "&transaction_num=" + transactionNum + "&description=" + description + "&search_helper=" + searchHelper, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "onErrorResponse: " + error);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(request);
}
}
}).start();
只要数据量小就没有问题,但是随着数据量的增加,软件的速度下降,过一段时间就会出现强行关闭的情况。
另外,为了从服务器接收数据,我使用以下代码,只要数据量很小,这不是问题,但是随着数据量的增加,软件速度下降,一段时间后,会发生强制关闭。
当数据量较小时,MainActivity 的 Intent 会在其自己的时间内发生,但当数据量较大时,Intent 启动速度比加载所有数据要快,因此 MainActivity 回收器视图为空。
StringRequest request = new StringRequest(Request.Method.GET, "http://myhost.ir/?command=get_partner_info&mobile=" + mobile, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("name");
String phoneNumber = mobile;
long percent = Long.parseLong(jsonObject.getString("percent"));
Partners partners = new Partners(name, phoneNumber, percent);
partnersDao.add(partners);
name = jsonObject.getString("partner_name");
phoneNumber = jsonObject.getString("partner_mobile");
percent = Long.parseLong(jsonObject.getString("partner_percent"));
Partners partners1 = new Partners(name, phoneNumber, percent);
partnersDao.add(partners1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "onErrorResponse: " + error);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(request);
StringRequest request1 = new StringRequest(Request.Method.GET, "http://myhost.ir/?command=get_transactions&mobile=" + mobile, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray transactionArray = new JSONArray(response);
for (int i = 0; i < transactionArray.length(); i++) {
JSONObject jsonObject1 = transactionArray.getJSONObject(i);
String traderName = jsonObject1.getString("tradername");
String operation = jsonObject1.getString("operation");
String from = jsonObject1.getString("from");
Double amount = Double.parseDouble(jsonObject1.getString("amount"));
String reason = jsonObject1.getString("reason");
Long transactionDate = Long.parseLong(jsonObject1.getString("transaction_date"));
Long registrationDate = Long.parseLong(jsonObject1.getString("registration_date"));
Long time = Long.parseLong(jsonObject1.getString("time"));
String transactionType = jsonObject1.getString("transaction_type");
Long transactionNum = Long.parseLong(jsonObject1.getString("transaction_num"));
String description = jsonObject1.getString("description");
int searchHelper = Integer.parseInt(jsonObject1.getString("search_helper"));
Log.e("TAG", "onResponse: " + "traderName: " + traderName + "operation: " + operation + "from: " + from + "amount: " + amount +
"reason: " + reason + "transDate: " + transactionDate + "registDate: " + registrationDate + "time: " + time + "transType: " + transactionType +
"transNum: " + transactionNum + "description: " + description + "searchHelper: " + searchHelper);
Transaction transaction = new Transaction(traderName, operation, from, amount, reason, transactionDate, registrationDate, time, transactionType, transactionNum, description, searchHelper);
transactionDao.add(transaction);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "onErrorResponse: " + error);
}
});
RequestQueue requestQueue1 = Volley.newRequestQueue(getContext());
requestQueue1.add(request1);
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
}
}, 200);
顺便说一下,我还没有创建静态RequestQueue,每个请求都会发送一个新的RequestQueue。这可能是我的软件问题吗?
I'm new to sending data to servers and working with the Volley Library.
I use the following code to send the data to the server using the Volley library in Splash Screen.
new Thread(new Runnable() {
@Override
public void run() {
List<Transaction> transactionsList = transactionDao.getAllTransactions();
for (int i = 0; i < transactionsList.size(); i++) {
Transaction transaction = transactionsList.get(i);
String mobile = partnersDao.selectPhoneNumberById(1);
String traderName = transaction.getTraderName();
String operation = transaction.getOperation();
String from = transaction.getFrom();
double amount = transaction.getAmount();
String reason = transaction.getReason();
long transactionDate = transaction.getTransactionDate();
long registrationDate = transaction.getRegistrationDate();
long time = transaction.getTime();
String transactionType = transaction.getTransactionType();
long transactionNum = transaction.getTransactionNum();
String description = transaction.getDescription();
int searchHelper = transaction.getSearchHelper();
StringRequest request = new StringRequest(Request.Method.POST, "http://myhost.ir/?command=set_transactions&mobile=" + mobile + "&trader_name=" + traderName + "&operation=" + operation + "&from=" + from + "&amount=" + amount + "&reason=" + reason + "&transaction_date=" + transactionDate + "®istration_date=" + registrationDate + "&time=" + time + "&transaction_type=" + transactionType + "&transaction_num=" + transactionNum + "&description=" + description + "&search_helper=" + searchHelper, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "onErrorResponse: " + error);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(request);
}
}
}).start();
As long as the amount of data is small, there is no problem, but as the number of data increases, the speed of the software decreases, and after a while, force close occurs.
Also, to receive data from the server, I use the following code, which is not a problem as long as the number of data is small, but with increasing the number of data, the software speed decreases and after a while, force close occurs.
When the number of data is small, intent to MainActivity occurs in its own time, but when the number of data is high, intent starts faster than loading all the data, and as a result, the MainActivity recycler view is empty.
StringRequest request = new StringRequest(Request.Method.GET, "http://myhost.ir/?command=get_partner_info&mobile=" + mobile, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String name = jsonObject.getString("name");
String phoneNumber = mobile;
long percent = Long.parseLong(jsonObject.getString("percent"));
Partners partners = new Partners(name, phoneNumber, percent);
partnersDao.add(partners);
name = jsonObject.getString("partner_name");
phoneNumber = jsonObject.getString("partner_mobile");
percent = Long.parseLong(jsonObject.getString("partner_percent"));
Partners partners1 = new Partners(name, phoneNumber, percent);
partnersDao.add(partners1);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "onErrorResponse: " + error);
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(request);
StringRequest request1 = new StringRequest(Request.Method.GET, "http://myhost.ir/?command=get_transactions&mobile=" + mobile, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray transactionArray = new JSONArray(response);
for (int i = 0; i < transactionArray.length(); i++) {
JSONObject jsonObject1 = transactionArray.getJSONObject(i);
String traderName = jsonObject1.getString("tradername");
String operation = jsonObject1.getString("operation");
String from = jsonObject1.getString("from");
Double amount = Double.parseDouble(jsonObject1.getString("amount"));
String reason = jsonObject1.getString("reason");
Long transactionDate = Long.parseLong(jsonObject1.getString("transaction_date"));
Long registrationDate = Long.parseLong(jsonObject1.getString("registration_date"));
Long time = Long.parseLong(jsonObject1.getString("time"));
String transactionType = jsonObject1.getString("transaction_type");
Long transactionNum = Long.parseLong(jsonObject1.getString("transaction_num"));
String description = jsonObject1.getString("description");
int searchHelper = Integer.parseInt(jsonObject1.getString("search_helper"));
Log.e("TAG", "onResponse: " + "traderName: " + traderName + "operation: " + operation + "from: " + from + "amount: " + amount +
"reason: " + reason + "transDate: " + transactionDate + "registDate: " + registrationDate + "time: " + time + "transType: " + transactionType +
"transNum: " + transactionNum + "description: " + description + "searchHelper: " + searchHelper);
Transaction transaction = new Transaction(traderName, operation, from, amount, reason, transactionDate, registrationDate, time, transactionType, transactionNum, description, searchHelper);
transactionDao.add(transaction);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", "onErrorResponse: " + error);
}
});
RequestQueue requestQueue1 = Volley.newRequestQueue(getContext());
requestQueue1.add(request1);
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
}
}, 200);
By the way, I have not created a static RequestQueue yet and a new RequestQueue will be sent for each request. Could this be my software problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您使用 Retrofit 库
方法论
因为这是关于测量任一库的“速度”。我只测量异步调用返回原始响应所需的时间。
我从本地新闻网站选择了 8 个 RSS 源,测量了每个库从全新安装中获取所有 8 个 RSS 源所需的平均时间。
为了考虑网络延迟和其他现实世界因素,我将每个测试运行了 10 次并对结果取平均值。
我对每个库进行了两次测量,以解决我第一次运行 10 次测试时可能发生的任何网络问题。
所有测试均在 Android Studio 2.0 x86 模拟器上运行,该模拟器在 Nexus 5 上运行 API 23。
对于 Volley,测试使用 OkHttp 3、Apache 4.4.12 和 Android 平台 API 运行(对于 API 9+,这是 URLConnection,对于旧版本,可能是 Apache HttpClient 2.0+)
POST 通过创建存根服务进行测试在我的本地主机上。对每次调用时间进行平均
结果
Retrofit 平均花费 41 毫秒来获取 8 个 API 响应。
改造缓存响应平均需要 21 毫秒
改造具有 3 个字段的 POST 平均花费 14 毫秒。
Volley-native 平均耗时 50 毫秒。
Volley 本机缓存响应平均需要 12 毫秒。
具有 3 个字段的 Volley 原生 POST 平均耗时 18 毫秒。
Volley-Apache4 平均耗时 48 毫秒。
Volley-Apache4 缓存响应平均需要 13 毫秒。
包含 3 个字段的 Volley-Apache4 POST 平均耗时 15 毫秒。
Volley-OkHttp3 平均耗时 40 毫秒。
Volley-OkHttp3 缓存响应平均耗时 14 毫秒。
Volley-OkHttp3 POST 包含 3 个字段,平均耗时 15 毫秒。
使用此链接了解更多信息:
https://medium.com/@ali.muzaffar/is-retrofit-faster-than-volley-the-answer-may-surprise-you-4379bc589d7c
I suggest you use Retrofit Library
Methodology
Since this is about measuring the “speed” of either library. I only measure the time is takes for an asynchronous call to return the raw response.
I selected 8 RSS feeds from a local news site measure the average amount of time it took for each library to fetch all 8 RSS feeds from a clean install.
In order to account of network latency and other real world factors, I ran each tests 10 times and averaged the results.
I measured each library twice to account for any network issues that may have been taking place the first time I ran the 10 tests.
All tests were run on the Android Studio 2.0 x86 emulator running API 23 on a Nexus 5.
For Volley, the tests were run using OkHttp 3, Apache 4.4.12 and Android platform API (for API 9+ this is URLConnection, for older versions it may be Apache HttpClient 2.0+)
POST was testing by creating a stubbed service on my localhost. Each call time was averaged
The result
Retrofit took on average 41ms to fetch the 8 API responses.
Retrofit cache responses took an average of 21ms
Retrofit POST with 3 fields took 14ms on average.
Volley-native took an average of 50ms.
Volley-native cache responses took an average of 12ms.
Volley-native POST with 3 fields took 18ms on average.
Volley-Apache4 took an average of 48ms.
Volley-Apache4 cache responses took an average of 13ms.
Volley-Apache4 POST with 3 fields took 15ms on average.
Volley-OkHttp3 took an average of 40ms.
Volley-OkHttp3 cache responses took an average of 14ms.
Volley-OkHttp3 POST with 3 fields took 15ms on average.
use this link for more information:
https://medium.com/@ali.muzaffar/is-retrofit-faster-than-volley-the-answer-may-surprise-you-4379bc589d7c