[英]Best way to handle error and success messages copy texts from back-end?
I have an Android application and right now I am managing error and success messages in strings.xml resource file. 我有一个Android应用程序,现在我正在管理strings.xml资源文件中的错误和成功消息。 Now in case if I want to change these messages I need to make changes in strings.xml file and give an app update in play-store, which is a bit overhead.
现在,如果我想更改这些消息,则需要在strings.xml文件中进行更改,并在Play商店中对应用程序进行更新,这有点麻烦。 Instead, I want these messages to be managed from server side (back-end) so that they can be changed without any update.
相反,我希望从服务器端(后端)管理这些消息,以便可以在不进行任何更新的情况下对其进行更改。 Can anyone please suggest me the best way to achieve this.
谁能建议我实现这一目标的最佳方法。
If you are use Volley then this is helpfil for you 如果您正在使用Volley,那么这对您来说是帮助
VolleyError is really just an extended Exception, so Exception.getMessage() likely wouldn't return what you are looking for unless you override the parsing methods for parsing your VolleyError in your extended Request class. VolleyError实际上只是扩展的Exception,因此Exception.getMessage()可能不会返回您要查找的内容,除非您在扩展的Request类中重写用于解析VolleyError的解析方法。 A really basic way to handle this would be to do something like:
处理此问题的一种非常基本的方法是执行以下操作:
//In your extended request class
@Override
protected VolleyError parseNetworkError(VolleyError volleyError){
if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
}
If you add this to your extended Request classes, your getMessage() should at least not return null. 如果将其添加到扩展的Request类中,则getMessage()至少不应返回null。 I normally don't really bother with this, though, since it's easy enough to do it all from within your onErrorResponse(VolleyError e) method.
不过,我通常并不为此而烦恼,因为从onErrorResponse(VolleyError e)方法中轻松完成所有操作就足够了。
You should use a JSON library to simplify things -- I use Gson for example or you could use Apache's JSONObjects which shouldn't require an additional external library. 您应该使用JSON库简化操作-例如,我使用Gson,也可以使用不需要额外外部库的Apache的JSONObjects。 The first step is to get the response JSON sent from your server as a String (in a similar fashion to what I just demonstrated), next you can optionally convert it to a JSONObject (using either apache's JSONObjects and JsonArrays, or another library of your choice) or just parse the String yourself.
第一步是获取从服务器以字符串形式发送的响应JSON(与我刚才演示的方式类似),接下来,您可以选择将其转换为JSONObject(使用apache的JSONObjects和JsonArrays或您的另一个库选择)还是自己解析String。 After that, you just have to display the Toast.
之后,您只需要显示Toast。
Here's some example code to get you started: 这是一些入门示例代码:
public void onErrorResponse(VolleyError error) {
String json = null;
NetworkResponse response = error.networkResponse;
if(response != null && response.data != null){
switch(response.statusCode){
case 400:
json = new String(response.data);
json = trimMessage(json, "message");
if(json != null) displayMessage(json);
break;
}
//Additional cases
}
}
public String trimMessage(String json, String key){
String trimmedString = null;
try{
JSONObject obj = new JSONObject(json);
trimmedString = obj.getString(key);
} catch(JSONException e){
e.printStackTrace();
return null;
}
return trimmedString;
}
//Somewhere that has access to a context
public void displayMessage(String toastString){
Toast.makeText(context, toastString, Toast.LENGTH_LONG).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.