0

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();
3
  • Where does it say "invalid return type"? Also, why do you even need this thread? You may want to look into AsyncTask if getProfile is something you want to run on a background thread.
    – Dennis K
    Apr 23, 2018 at 19:12
  • I tried with Async task and then gave up,if (getActivity()!=null) here it says.If you can turn it in a async task its okay for my @DennisK
    – ora
    Apr 23, 2018 at 19:13
  • 1
    Ok, I'll post a possible solution with AsyncTask. But you need to learn the tools of the trade. Using Threads directly is not trivial in android and you will likely cause more issues if you try.
    – Dennis K
    Apr 23, 2018 at 19:16

1 Answer 1

2

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();
7
  • Sorry, omitted (). Try now
    – Dennis K
    Apr 23, 2018 at 19:33
  • it needs the doInBackground() params
    – ora
    Apr 23, 2018 at 19:34
  • Sorry, I'm not at the dev PC. Fixed. Also you could just use IDE's autosuggestion to implement methods.
    – Dennis K
    Apr 23, 2018 at 19:38
  • the method looks okay but throws java.net.UnknownHostException: Unable to resolve host "gateway.watsonplatform.net": No address associated with hostname
    – ora
    Apr 23, 2018 at 19:41
  • Well, that's your service not able to connect. You didn't provide this code and it would probably have to be a new question if you cannot figure it out yourself.
    – Dennis K
    Apr 23, 2018 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.