从try catch块访问字符串值(Access string value from try catch block)

编程入门 行业动态 更新时间:2024-10-21 04:10:53
从try catch块访问字符串值(Access string value from try catch block)

我在OnCreate中调用一个方法如下

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); GetScore();

该方法在此定义

private void GetScore() { AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.put("email", textEmail); client.get("http://techcube.pk/game/getScore.php", params, new `enter code here`AsyncHttpResponseHandler() { @Override public void onSuccess(String s) { super.onSuccess("Success"); String score = null; try { Log.d("Success", s); JSONObject json = new JSONObject(s); score = json.getString("Score"); } catch (Exception e) { e.printStackTrace(); } //Toast.makeText(getApplicationContext(), score, Toast.LENGTH_LONG).show(); fetchlivescore.setText(score); } @Override public void onFailure(Throwable throwable) { super.onFailure(throwable); Toast.makeText(getApplicationContext(), "Server Response for onFailure ", Toast.LENGTH_LONG).show(); } }); }

现在我要做的是将json字符串的值存储在onSuccess方法内的fetchlivescore textview中,但我也想在Oncreate方法上使用这个值,即当我下次打开应用程序时显示toast或包含与Onsuccess方法中存储的值相同。 我怎么能实现这个目标? 我已经尝试了很多东西,但没有任何作用,因为下次在oncreate方法中没有显示存储的值,它总是显示为空

I am calling a method in OnCreate as following

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); GetScore();

The method is defined here

private void GetScore() { AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.put("email", textEmail); client.get("http://techcube.pk/game/getScore.php", params, new `enter code here`AsyncHttpResponseHandler() { @Override public void onSuccess(String s) { super.onSuccess("Success"); String score = null; try { Log.d("Success", s); JSONObject json = new JSONObject(s); score = json.getString("Score"); } catch (Exception e) { e.printStackTrace(); } //Toast.makeText(getApplicationContext(), score, Toast.LENGTH_LONG).show(); fetchlivescore.setText(score); } @Override public void onFailure(Throwable throwable) { super.onFailure(throwable); Toast.makeText(getApplicationContext(), "Server Response for onFailure ", Toast.LENGTH_LONG).show(); } }); }

Now what i want to do is that i store the value of json string in fetchlivescore textview inside onSuccess method but i want to use this value also on the Oncreate method i.e. when i open the app next time it show a toast or a textview containing the same value that was stored in the Onsuccess method. how can i achieve this? i have tried many things but nothing works because the stored value is not showing next time in oncreate method and it always show empty

最满意答案

保存分数的一种方法是在onSuccess()方法中使用SharedPreferences。

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("key_score", score); editor.commit();

在onCreate()您只需读取存储的值。

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); String score = sharedPref.getString("key_score");

如果你在一个Activity中 - 使用this而不是getActivity() 。

One way to save the score would be to use SharedPreferences in your onSuccess() method.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("key_score", score); editor.commit();

And in onCreate() you just read the value stored.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); String score = sharedPref.getString("key_score");

If you're inside an Activity - use this instead of getActivity().

更多推荐

本文发布于:2023-04-29 12:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串值   catch   Access   block   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!