2016-11-05 10 views
0

主な問題は、2つの値のヘルプを返すことができないことです。私は多くの時間をかけたが成功しなかった。そして、私はこれに新しいですので、私のコードのおかげであなたの答えを書いてください前におかげで。asyncTaskを使用して複数のURLからデータを解析する方法

ここに私のコード

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

初になり、あなたは良い質問を書くことを学ぶ必要がある - 時間内[尋ねる方法](http://stackoverflow.com/help/how-to-ask)のセクションを読んでくださいエル・P。コード・ダンプ全体ではなく、関連するコードのみを含めてください。 –

答えて

0

はあなたがStringにすべてを詰め込むことを試みるべきではないのです。複数の値を保持するために、より良いデータ構造があります:配列などのベクトル、リスト、としてあなたAsyntTaskを宣言します。

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

してからdoInBackgorund方法は、このようなものになるだろう:

@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; 
} 

そして最後にあなたのonPostExecuteは、すべての

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

ありがとう、ちょうど私のURLを追加する場所を教えてください –

+0

あなたは自分で把握する必要があります。 –

+0

ありがとうAleksそれは正常に動作します。 –

関連する問題