没有JSON输出?

时间:2017-10-23 16:19:11

标签: java android json currency

我正在编写货币兑换Android应用程序,但是当应用程序运行时,我似乎没有得到API的响应。

API LINK 1)https://restcountries.eu/rest/v2/all

API LINK 2)http://api.fixer.io/latest

MainActivity Code:

 import android.os.AsyncTask;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;

 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;

 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;

 import static java.lang.reflect.Modifier.FINAL;

 public class MainActivity extends AppCompatActivity {


//Fill spinner with values from this URL - https://restcountries.eu/rest/v2/all

final String CURRENCY_DATA = "https://restcountries.eu/rest/v2/all";

ArrayList<String> currency_list = new ArrayList();

Button btn;

//The amount entered x's by the curreny exchange rate..

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

    btn = (Button) findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            new downloadData().execute();

            new downloadExchangeRate().execute();

        }
    });

}




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

    String result = "";



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


        try {
            URL url = new URL(CURRENCY_DATA);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.connect();
            InputStream inputStream = httpURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

            int data = inputStreamReader.read();

            while (data != -1) {

                char current = (char) data;

                result += current;

                data = inputStreamReader.read();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onPostExecute(String s) {

        try {
            JSONObject jsonObject = new JSONObject(result);
            JSONArray jsonArray = jsonObject.getJSONArray("currencies");


            for (int i =0; i<jsonArray.length(); i++){


                currency_list.add(jsonArray.getJSONObject(i).optString("code"));

                Log.d("list", currency_list.toString());

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

        super.onPostExecute(s);
    }
}



 }

downloadExchangeRate.java代码:

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;


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


String result = "";

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

    final String EXCHANGE_RATE = "http://api.fixer.io/latest";

    try {
        URL url = new URL(EXCHANGE_RATE);
        HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
        httpURLConnection.connect();
        InputStream inputStream = httpURLConnection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

        int data = inputStreamReader.read();

        while (data != -1) {

            char current = (char) data;

            result += current;

            data = inputStreamReader.read();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}


@Override
protected void onPostExecute(String s) {

    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(result);

    for (int i = 0; i<jsonObject.length(); i++){

        ArrayList<String> rates = new ArrayList<>();
        rates.add(jsonObject.optString("rates"));

        Log.d("list", rates.toString());

    }

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




    super.onPostExecute(s);


}
}

感谢任何帮助,我认为我称JSON错误。

1 个答案:

答案 0 :(得分:0)

将代码更新为:

使用StringBuilder获取结果(为什么?请参阅此https://stackoverflow.com/a/14927864/5227589):

StringBuilder result=new StringBuilder();
...
......
result.append(current);

使用String时,由于使用result += current;,完成while循环需要很长时间:。

String Concatenating Java (Android)

String Concatenating Java (Android) Memory CPU Problem

使用StringBuilder时:

Used StringBuilder Java (Android)

在onPostExecute

JSONArray jsonArray = new JSONArray(result.toString());

                for (int i =0; i<jsonArray.length(); i++){

                    currency_list.add(jsonArray.getJSONObject(i).getJSONArray("currencies").getJSONObject(0).optString("code"));

                    //Log.d("list", currency_list.toString());

                }

完整代码:

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

        StringBuilder result=new StringBuilder();



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


            try {
                URL url = new URL(CURRENCY_DATA);
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                int data = inputStreamReader.read();
                char current = (char) data;
                Log.i("data",current+"");
                while (data != -1) {

                    current = (char) data;

                    result.append(current);

                    data = inputStreamReader.read();
                  //  Log.i("data",current+"");
                }
                //Log.i("data",result+"");

            } catch (IOException e) {
                e.printStackTrace();
            }
            return result.toString();
        }

        @Override
        protected void onPostExecute(String s) {

            try {
                JSONArray jsonArray = new JSONArray(result.toString());

                for (int i =0; i<jsonArray.length(); i++){

                    currency_list.add(jsonArray.getJSONObject(i).getJSONArray("currencies").getJSONObject(0).optString("code"));

                    //Log.d("list", currency_list.toString());

                }
                Log.d("list", currency_list.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            super.onPostExecute(s);
        }
    }