0

Excuse me I try to learn this topic and this topic. But I can't pass this problem. I want to use result from msg and use this result to show in textview. How can I get the result and use this.

interface class

public interface AsyncResponse {
 void processR(String output);}

this main class

public class Exchange extends Activity implements AsyncResponse {

    String url = "abcd"

    public String getRatesData(String strUrl) {
        String strResult = "";
        try {
            URL url = new URL(strUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            strResult = readStream(con.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strResult;
    }

  public String readStream(InputStream in) {
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

  public class GetRatesTask extends AsyncTask<String, Void, String> {

      public AsyncResponse delegate = null;

      @Override
      public String doInBackground(String... urls) {
          return getRatesData(urls[0]);
      }
@Override
      public void onPostExecute(String jsonString) {
          String msg = "";

          try {
              JSONObject json = new JSONObject(jsonString);
              JSONObject val = json.getJSONObject("rates");                
            msg += String.format("%s\n", val.getString("SGD"));  // << use this result
            delegate.processR(msg);  

          } catch (JSONException e) {
              e.printStackTrace();
          }



      }
  }

      GetRatesTask task = new GetRatesTask();
      @Override
        public void onCreate(Bundle savedInstanceState) 
    {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.exchange);  

       task.delegate = this;

            }

      void processR(String output){

          TextView txt1 = (TextView)findViewById(R.id.textView1);
          txt1.setText(output);   <<< show in this textview
      }

}
Community
  • 1
  • 1
Flyzy Dev
  • 91
  • 1
  • 3
  • 10
  • 1
    Have you done a search for this on StackOverflow? Try http://stackoverflow.com/a/21540553/763080 – Phil Feb 13 '14 at 19:17
  • You have `processresult` in your `interface` but `processR` in your `Activity` and `AsyncTask`. They should be the same name. Can you explain where it fails? Have you done any debugging? – codeMagic Feb 13 '14 at 19:40

0 Answers0