如何在Java中等待函数执行完成?

huangapple 未分类评论49阅读模式
英文:

How can I wait a function to finish in Java?

问题

public void getCards(){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GETC_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if(!response.contains("Error!")){
                        JSONArray jsonArray;
                        JSONObject jsonObject;
                        String ccnumber;
                        String fullname;
                        String ccsignature;
                        try {
                            jsonArray = new JSONArray(response);
                            jsonObject = jsonArray.getJSONObject(0);
                            ccnumber = jsonObject.getString("ccNumber");
                            ccsignature = jsonObject.getString("ccSign");
                            fullname = jsonObject.getString("fullName");
                            editor.putString("ccnumber", ccnumber);
                            editor.putString("ccsignature", ccsignature);
                            editor.putString("fullname", fullname);
                            editor.commit();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }else{
                        Toast.makeText(FinalUserSpace.this, response, Toast.LENGTH_SHORT).show();
                    }
                }
            }, new Response.ErrorListener() {
getCards();

ccnr = pref.getString("ccnumber", null);
ccfn = pref.getString("fullname", null);
if(ccfn == null || ccnr == null){
    tvccnumber.setText("No card was added!");
    tvccfullname.setText("No card was added!");
}else{
    tvccnumber.setText(ccnr);
    tvccfullname.setText(ccfn);
}
英文:

I'm developing an Android app in Java that needs to load some data from a remote database. For that I have a function called getCards() that reads the DB and loads some information into SharedPreferences. In my main function I want to read those infromation written in the SharedPreferencesand write them on a TextView. The thing is that when I try to get those information from SharedPreferencesthey seem to be null because the thread does not wait for "getCards()" to finish. If i go into another activity and then come back in the main activity, the data will be put, but when I first start the activity the read from SharedPreferencesis too fast. Any suggestions? I've tried with wait(100) surrounded by try/catch, but the wapp would crash.

This is the code from the main activity.

        getCards();
       
        ccnr = pref.getString(&quot;ccnumber&quot;,null);
        ccfn = pref.getString(&quot;fullname&quot;,null);
        if(ccfn == null || ccnr ==null){
            tvccnumber.setText(&quot;No card was added!&quot;);
            tvccfullname.setText(&quot;No card was added!&quot;);

        }else{
            tvccnumber.setText(ccnr);
            tvccfullname.setText(ccfn);
        }

This is the part of getCards() that loads the information in SharedPreferences.

public void getCards(){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GETC_URL,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        if(!response.contains(&quot;Error!&quot;)){
                            JSONArray jsonArray;
                            JSONObject jsonObject;
                            String ccnumber;
                            String fullname;
                            String ccsignature;
                            try {
                                jsonArray = new JSONArray(response);
                                jsonObject = jsonArray.getJSONObject(0);
                                ccnumber = jsonObject.getString(&quot;ccNumber&quot;);
                                ccsignature = jsonObject.getString(&quot;ccSign&quot;);
                                fullname = jsonObject.getString(&quot;fullName&quot;);
                                editor.putString(&quot;ccnumber&quot;,ccnumber);
                                editor.putString(&quot;ccsignature&quot;,ccsignature);
                                editor.putString(&quot;fullname&quot;,fullname);
                                editor.commit();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }else{
                            Toast.makeText(FinalUserSpace.this, response, Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Response.ErrorListener() {
            

答案1

得分: 0

以下是您要求的翻译内容:

您可以使用一个 Java 对象进行锁定,直到函数返回。

private Object lock = new Object();
public void getcards(){
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GETC_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if(!response.contains("Error!")){
                        JSONArray jsonArray;
                        JSONObject jsonObject;
                        String ccnumber;
                        String fullname;
                        String ccsignature;
                        try {
                            jsonArray = new JSONArray(response);
                            jsonObject = jsonArray.getJSONObject(0);
                            ccnumber = jsonObject.getString("ccNumber");
                            ccsignature = jsonObject.getString("ccSign");
                            fullname = jsonObject.getString("fullName");
                            editor.putString("ccnumber",ccnumber);
                            editor.putString("ccsignature",ccsignature);
                            editor.putString("fullname",fullname);
                            editor.commit();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }else{
                        Toast.makeText(FinalUserSpace.this, response, Toast.LENGTH_SHORT).show();
                    }

                    lock.notifyAll();
                }
            }, new Response.ErrorListener() {
getcards();
       
lock.wait();

ccnr = pref.getString("ccnumber",null);
ccfn = pref.getString("fullname",null);
if(ccfn == null || ccnr ==null){
    tvccnumber.setText("No card was added!");
    tvccfullname.setText("No card was added!");
}else{
    tvccnumber.setText(ccnr);
    tvccfullname.setText(ccfn);
}
英文:

You may use a java object to lock until the fonction return.

private Object lock = new Object();
public void getcards(){
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GETC_URL,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        if(!response.contains(&quot;Error!&quot;)){
                            JSONArray jsonArray;
                            JSONObject jsonObject;
                            String ccnumber;
                            String fullname;
                            String ccsignature;
                            try {
                                jsonArray = new JSONArray(response);
                                jsonObject = jsonArray.getJSONObject(0);
                                ccnumber = jsonObject.getString(&quot;ccNumber&quot;);
                                ccsignature = jsonObject.getString(&quot;ccSign&quot;);
                                fullname = jsonObject.getString(&quot;fullName&quot;);
                                editor.putString(&quot;ccnumber&quot;,ccnumber);
                                editor.putString(&quot;ccsignature&quot;,ccsignature);
                                editor.putString(&quot;fullname&quot;,fullname);
                                editor.commit();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }else{
                            Toast.makeText(FinalUserSpace.this, response, Toast.LENGTH_SHORT).show();
                        }

                        lock.notifyAll();
                    }
                }, new Response.ErrorListener() {
getcards();
       
lock.wait();

ccnr = pref.getString(&quot;ccnumber&quot;,null);
ccfn = pref.getString(&quot;fullname&quot;,null);
if(ccfn == null || ccnr ==null){
    tvccnumber.setText(&quot;No card was added!&quot;);
    tvccfullname.setText(&quot;No card was added!&quot;);
}else{
    tvccnumber.setText(ccnr);
    tvccfullname.setText(ccfn);
}

huangapple
  • 本文由 发表于 2020年6月29日 17:37:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/62635282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定