Invalid return type fragment thread

2019-08-30 04:44发布

I have created a Thread in Android IDK why it says invalid return type! All I want is to display the json I get from Profile I want to change the text in my fragment

Thread thread= new Thread(){
          if (getActivity()!=null)
          {
              getActivity().runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      Profile profile = service.getProfile(text);
                      Gson gson = new Gson();
                      String json = gson.toJson(profile);
                      output.setText(json.toString());
                  }
              });
          }
      };
        thread.start();

1条回答
Ridiculous、
2楼-- · 2019-08-30 05:33

Try this

    new AsyncTask<Void, Void, Void> (){
        private String json;

        protected Void doInBackground(Void... voids) {
            Profile profile = service.getProfile(text);
            Gson gson = new Gson();
            json = gson.toJson(profile);
            return null;
        }

        protected void onPostExecute(Void result) {
            output.setText(json);
        }
    }.execute();
查看更多
登录 后发表回答