2016-11-05 5 views
0

Das Hauptproblem ist, dass ich zwei Wertehilfe nicht zurückgeben kann. Ich habe viel Zeit versucht, aber keinen Erfolg. Und Leute, die ich neu bin, also schreibe bitte deine Antwort in Bezug auf meinen Code, danke im Voraus.So analysieren Sie Daten von mehreren URLs mithilfe von asyncTask

Hier ist mein Code

public class calculate extends AsyncTask<String, String, String> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 

      try { 
       uss = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22INRUSD%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="); 
       JSONObject usjObj; 
       usjObj = new JSONObject(uss); 
       usResult = usjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate"); 


       eurr = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22INREUR%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="); 
       JSONObject eurjObj; 
       eurjObj = new JSONObject(eurr); 
       eurResult = eurjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return eurResult + usResult; 
      ////PROBLEM IS HERE ACTUALLY I DON'T KNOW HOW TO RETURN TWO OR MORE VALUE/////" 
     } 

     @Override 
     protected void onPostExecute(String usResult) { 
      valueus = Double.parseDouble(usResult); 
      inputus = lengthvalue * valueus; 
      us.setText("" + inputus); 

      valueeur = Double.parseDouble(eurResult); 
      inputeur = lengthvalue * valueeur; 
      eur.setText("" + inputeur); 
     } 

    } 


    public String getJson(String url) throws ClientProtocolException, IOException { 

     StringBuilder build = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     HttpResponse response = client.execute(httpGet); 
     HttpEntity entity = response.getEntity(); 
     InputStream content = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
     String con; 
     while ((con = reader.readLine()) != null) { 
      build.append(con); 
     } 
     return build.toString(); 
    } 
} 
+0

zu allererst wäre, müssen Sie gute Fragen schreiben lernen - lesen Sie [Wie fragen] (http://stackoverflow.com/help/how-to-ask) Abschnitt in h elfenbein. Enthalten Sie nur relevanten Code, nicht Ihren gesamten Code-Dump. –

Antwort

0

Sie sollten nicht versuchen, alles in eine String zu stopfen. Es gibt bessere Datenstrukturen mehrere Werte zu halten: Array, Vektor, List, usw. Erklären Sie Ihre AsyntTask als:

public class calculate extends AsyncTask<String, String, String[]> 

und dann doInBackgorund Methode so etwas wie dieses würde:

@Override 
protected String doInBackground(String... params) { 
    String[] result = new String[numResults]; 

    ... 
    result[0] = usjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate"); 

    ... 
    result[1] = usjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate"); 

    ... 

    return result; 
} 

Und schließlich Ihre onPostExecute

@Override 
protected void onPostExecute(String[] usResult) { 
    ... 
} 
+0

Dank sagen Sie mir einfach, wo ich meine URLs –

+0

hinzufügen, dass Sie selbst herausfinden müssen. –

+0

Dank Aleks funktioniert es gut –

Verwandte Themen