我如何在Volley中的响应后/之后更新TextView

时间:2019-07-09 19:59:28

标签: android-studio textview android-volley

我是Android Studio初学者。我正在使用Volley从服务器下载JSON对象。没问题。我希望在JSON中下载的字符串在TextViews中显示。我已经尝试过以响应方式直接更新它们,并且尝试了很多事情,但是我肯定缺少一些东西。他们不会更新。如果我按下按钮“ knapp”,它调用在开始时运行的相同Getpostdata,则textviews更新就好了。希望任何人都可以告诉我我在这里做错了什么。预先感谢。

enter code here

public class MainActivity extends AppCompatActivity {
firebasetokenID tokenapp= new firebasetokenID();
String s;

TextView tvlegesenter;
TextView tvsistaktiv;
TextView tvstatus;
TextView tvlege;
Button knapp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
knapp=(Button) findViewById(R.id.button);
tvlege= (TextView) findViewById(R.id.TVLegen);
tvlegesenter= (TextView) findViewById(R.id.TVLegesenter);
tvsistaktiv= (TextView) findViewById(R.id.TVSistaktiv);
tvstatus= (TextView) findViewById(R.id.TVstatus);
tvlegesenter.setText("");
tvlege.setText("");
tvsistaktiv.setText("");
tvstatus.setText((""));
knapp.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
      getPostData();
  }
});
           FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MainActivity.this, new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            s = instanceIdResult.getToken();

        }
    });
getPostData();
}


public void getPostData() {

 final   ArrayList<String> liste = new ArrayList<String>();

    {
        final String URL = "https://xxx.no/xxx.php";

final StringRequest stringRequest = new   

StringRequest(Request.Method.POST, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        final String s = response.toString();

                   ArrayList<String> aList = new ArrayList<String>();

                        try {

                            JSONObject object = new JSONObject(s);
                            Iterator keys = object.keys();
                            while(keys.hasNext()) {
                          String dynamicKey =   (String)keys.next();
                            liste.add(object.getString(dynamicKey));

                            }
                            String y;
                            String u;
                            String o;
                            String p;
                            y=liste.get(0);
                            u=liste.get(1);
                            p=liste.get(2);
                            o=liste.get(3);

                            try {
                               updateTextView(y,u,p,o);

                                // tekst(y,u,o,p);
                            } catch (Exception e) {
                                e.printStackTrace();
                                String hh;
                                hh="a";

                            }

                        } catch (JSONException e) {
                            String sss;
                            sss="aaa";
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        String dd;
                        dd="aa";
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("a", s);
                return hashMap;
            }

        };
        final RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
        requestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<Object>() {
            @Override
            public void onRequestFinished(Request<Object> request) {
                requestQueue.getCache().clear();
            }
        });
    }


}
private void updateTextView(final String s, final String s1, final String s2, final String s3) {
    MainActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
           // TextView tv= (TextView) findViewById(R.id.TVstatus);
            tvlege.setText("Text"+s);
           tvlegesenter.append("Text"+s1);
            tvsistaktiv.append("Text "+s2);
            tvstatus.append("Text "+s3);
        }
    });

}

}

0 个答案:

没有答案