如何从Server中的xml文件中检索实时货币信息

时间:2017-01-11 16:48:26

标签: java android json xml post

我想将这个表单的xml文件从url解析到我的android应用程序中。

[{"currencyId":53,"buy":21.23042,"sell":21.1921,"name":"USD/MXN","pipMultiplier":10000},{"currencyId":22,"buy":1.50225,"sell":1.49891,"name":"EUR/NZD","pipMultiplier":10000},{"currencyId":15,"buy":1.38201,"sell":1.38083,"name":"EUR/CAD","pipMultiplier":10000},{"currencyId":62,"buy":0.69619,"sell":0.69404,"name":"NZD/CHF","pipMultiplier":10000},{"currencyId":19,"buy":80.085,"sell":79.968,"name":"NZD/JPY","pipMultiplier":100},{"currencyId":13,"buy":1.23727,"sell":1.23613,"name":"GBP/CHF","pipMultiplier":10000},{"currencyId":16,"buy":0.95424,"sell":0.95167,"name":"AUD/CAD","pipMultiplier":10000},{"currencyId":5,"buy":1.00469,"sell":1.00414,"name":"USD/CHF","pipMultiplier":10000},{"currencyId":1,"buy":122.014,"sell":121.839,"name":"EUR/JPY","pipMultiplier":100},{"currencyId":60,"buy":0.9092,"sell":0.90615,"name":"NZD/CAD","pipMultiplier":10000},{"currencyId":97,"buy":30.806,"sell":30.793,"name":"TRY/JPY","pipMultiplier":100},{"currencyId":10,"buy":0.68269,"sell":0.6823,"name":"NZD/USD","pipMultiplier":10000},{"currencyId":23,"buy":1.61316,"sell":1.61245,"name":"GBP/CAD","pipMultiplier":10000},{"currencyId":9,"buy":1.31115,"sell":1.31023,"name":"USD/CAD","pipMultiplier":10000},{"currencyId":3,"buy":1.21594,"sell":1.21552,"name":"GBP/USD","pipMultiplier":10000},{"currencyId":27,"buy":1.75536,"sell":1.7494,"name":"GBP/NZD","pipMultiplier":10000},{"currencyId":63,"buy":3.6378,"sell":3.62206,"name":"USD/TRY","pipMultiplier":10000},{"currencyId":64,"buy":3.83117,"sell":3.81819,"name":"EUR/TRY","pipMultiplier":10000},{"currencyId":28,"buy":0.73074,"sell":0.72814,"name":"AUD/CHF","pipMultiplier":10000},{"currencyId":50,"buy":9.54563,"sell":9.51557,"name":"EUR/SEK","pipMultiplier":10000},{"currencyId":18,"buy":87.091,"sell":86.919,"name":"CAD/JPY","pipMultiplier":100},{"currencyId":14,"buy":1.43031,"sell":1.4289,"name":"EUR/AUD","pipMultiplier":10000},{"currencyId":8,"buy":0.71693,"sell":0.7154,"name":"AUD/USD","pipMultiplier":10000},{"currencyId":17,"buy":83.97,"sell":83.811,"name":"AUD/JPY","pipMultiplier":100},{"currencyId":11,"buy":0.84385,"sell":0.84329,"name":"EUR/GBP","pipMultiplier":10000},{"currencyId":4,"buy":115.641,"sell":115.555,"name":"USD/JPY","pipMultiplier":100},{"currencyId":61,"buy":0.75641,"sell":0.75508,"name":"CAD/CHF","pipMultiplier":10000},{"currencyId":20,"buy":1.67121,"sell":1.66758,"name":"GBP/AUD","pipMultiplier":10000},{"currencyId":76,"buy":1171.7,"sell":1170.674,"name":"XAU/USD","pipMultiplier":100},{"currencyId":6,"buy":142.45,"sell":142.188,"name":"GBP/JPY","pipMultiplier":100},{"currencyId":2,"buy":1.04081,"sell":1.03929,"name":"EUR/USD","pipMultiplier":10000},{"currencyId":54,"buy":13.77633,"sell":13.70746,"name":"USD/ZAR","pipMultiplier":10000},{"currencyId":7,"buy":1.05845,"sell":1.05868,"name":"EUR/CHF","pipMultiplier":10000},{"currencyId":52,"buy":8.53823,"sell":8.51386,"name":"USD/NOK","pipMultiplier":10000},{"currencyId":48,"buy":9.0615,"sell":9.03791,"name":"USD/SEK","pipMultiplier":10000},{"currencyId":12,"buy":113.73,"sell":113.454,"name":"CHF/JPY","pipMultiplier":100},{"currencyId":21,"buy":1.03599,"sell":1.03438,"name":"AUD/NZD","pipMultiplier":10000},{"currencyId":51,"buy":8.99332,"sell":8.96375,"name":"EUR/NOK","pipMultiplier":10000},{"currencyId":150,"buy":6.7165,"sell":6.7144,"name":"USD/CNH","pipMultiplier":1000},{"currencyId":77,"buy":15.142,"sell":15.112,"name":"XAG/USD","pipMultiplier":100},{"currencyId":93,"buy":7.182,"sell":7.129,"name":"ZAR/JPY","pipMultiplier":100}]

这是一系列货币。

经过大量的努力,我设法将xml文件解析为我的应用程序中的字符串。 该字符串现在具有以下形式。

 import android.os.AsyncTask;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.net.URLConnection;


 public class MainActivity extends AppCompatActivity {

String str = "http://massignment.zulutrade.com/api/rates";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new AsyncTaskClass().execute(str);

}




class AsyncTaskClass extends AsyncTask<String, Void,String >  {

    private Exception exception;

    protected String doInBackground(String ...params) {

        URLConnection connection = null;
        BufferedReader reader = null;

        try {

            URL url = new URL(str);

            connection = url.openConnection();
            connection.setDoOutput(true);

            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            String line = "";

            StringBuilder builder = new StringBuilder();
            int i=0;
            while ((line = reader.readLine()) != null) {

                builder.append(line+"\n");

                // Log.w("Response  ", "> " + line );
                i++;
            }
            String xml = builder.toString();


        }

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

        return  "0k";
    }

    protected void onPostExecute() {

    }

}

}

使用以下代码:

(Select Value from UserDefinedFieldLinks where UserDefinedFieldID = 135)
(Select Value from UserDefinedFieldLinks where UserDefinedFieldID = 137)

我需要将xml / string解析为其内容。 知道我该如何解析?

1 个答案:

答案 0 :(得分:0)

假设您的代码是Java代码 - 有一些很好的XML解析器可用。您可能希望使用其中一个来解析XML。