从URL API Android Studio检索数据

时间:2018-12-10 23:05:15

标签: android json

我正在尝试仅将地址作为数组中的项检索,并从此postcode API

中将其显示在微调器中

包含JSON的line变量可以正确显示,但是当我尝试仅显示并显示地址时,甚至不会到达代码的那一部分。

这是我的代码:

private class getJsonData extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(LocationActivity.this);
        pd.setMessage("Please wait");
        pd.setCancelable(false);
        pd.show();
    }

    protected String doInBackground(String... params) {


        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

                jsonArray = new JSONArray(line);
                for(int i = 0; i < jsonArray.length(); i++ ){
                    jsonObject = jsonArray.getJSONObject(i);
                    Log.d("Output", jsonObject.getString("addresses"));
                }

            }



            return buffer.toString();

        }catch(Exception e){
            Log.e("Error", e.getMessage());
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pd.isShowing()){
            pd.dismiss();
        }

       Spinner addressSpinner = (Spinner) findViewById(R.id.addressSpinner);
        String[] cacat = {"cacat1", "cacat2"};
        addressSpinner.setAdapter(new ArrayAdapter<String>(LocationActivity.this,
                android.R.layout.simple_spinner_dropdown_item,cacat));

        addressSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

这是在我的onCreate函数中:

    postcodeEdit = (EditText) findViewById(R.id.postcodeEdit);
    townEdit = (EditText) findViewById(R.id.townEdit);
    getAddresses = (Button) findViewById(R.id.findAddressBtn);

    getAddresses.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new getJsonData().execute("https://api.getAddress.io/find/" + postcodeEdit.getText().toString());
        }
    });

它成功显示所有JSON,但是当我尝试仅获取地址并将其插入我的ArrayList时,它说ArrayList为空

0 个答案:

没有答案