如何获取根据 JAVA 中的用户选择而变化的 JsonObjects?

Posted

技术标签:

【中文标题】如何获取根据 JAVA 中的用户选择而变化的 JsonObjects?【英文标题】:How can I fetch JsonObjects that change according to user selection in JAVA? 【发布时间】:2021-07-13 06:11:12 【问题描述】:

我正在使用免费货币转换器 API 开发货币转换器应用程序,我设法将 JSON 提取到 Logcat 中,但我需要从中获取 JSON 对象的值 这是 JSON:


  "USD_INR": 74.61395

印度卢比和美元会根据用户的选择而变化,所以我怎样才能将数据输入我的应用程序,如下所示: 示例:

weatherData.mCity = jsonObject.getString("name");

这是我的代码:

package com.example.currencyconverter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;

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

import cz.msebera.android.httpclient.Header;

import static com.loopj.android.http.AsyncHttpClient.log;

public class MainActivity extends AppCompatActivity 
//    https://free.currconv.com/api/v7/convert?q=USD_INR&compact=ultra&apiKey=38b9f7e70c1b1eb6089e


    private String URL = "https://free.currconv.com/api/v7/convert?q=";
    private final String URL_BALANCE = "&compact=ultra&apiKey=38b9f7e70c1b1eb6089e";
    private final String API_KEY = "38b9f7e70c1b1eb6089e";
    String result;
    String[] currency1;
    String[] currency2;
    String name;

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

        Spinner spinner1 = findViewById(R.id.spinner1);
        Spinner spinner2 = findViewById(R.id.spinner2);
        Button myButton = findViewById(R.id.button);

        currency1 = new String[1];
        currency2 = new String[1];



        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency_array, R.layout.custom_spinner);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner1.setAdapter(adapter);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
                currency1[0] = parent.getItemAtPosition(position).toString();
                Log.d("myApp", "currency:" + currency1[0]);
                name = '\u0022' + currency1[0] + "_" + currency2[0] + '\u0022';


            

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

            
        );

        spinner2.setAdapter(adapter);
        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
                currency2[0] = parent.getItemAtPosition(position).toString();
                Log.d("myApp", "currency:" + currency2[0]);
                name = '\u0022' + currency1[0] + "_" + currency2[0] + '\u0022';

            

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

            
        );

        myButton.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 



                String finalURL = URL + currency1[0] + "_" + currency2[0] + URL_BALANCE;

                log.d("myApp", "url:" + finalURL);
                Log.d("myApp", "json:" + name);


                networking(finalURL);


            
        );


    


    private void networking(String url) 
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(url, new JsonHttpResponseHandler() 

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) 
                Log.d("myApp", "JSON" + response.toString());
                try 

                    

                    result = response.getString(name);
                    Log.d("myApp", "result" + result);


                 catch (JSONException e) 
                    e.printStackTrace();

                

            

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) 
                Log.d("myApp", "Request Failed:" + statusCode);
                Log.d("myApp", "Response Fail:" + errorResponse);
                Log.d("myApp", "Error:" + throwable.toString());

            
        );
    



这是我得到的输出:

2021-04-19 08:17:00.803 16457-16457/com.example.currencyconverter D/myApp: currency:USD
2021-04-19 08:17:03.877 16457-16457/com.example.currencyconverter D/myApp: currency:INR
2021-04-19 08:17:04.962 16457-16457/com.example.currencyconverter V/myApp: url:https://free.currconv.com/api/v7/convert?q=USD_INR&compact=ultra&apiKey=38b9f7e70c1b1eb6089e
2021-04-19 08:17:04.962 16457-16457/com.example.currencyconverter D/myApp: json:"USD_INR"
2021-04-19 08:17:06.287 16457-16457/com.example.currencyconverter D/myApp: JSON"USD_INR":74.61395

【问题讨论】:

【参考方案1】:

使用 jsonObject.keys() 获取 JSON 对象的所有键。然后您可以循环返回值并请求与该键关联的值。

【讨论】:

【参考方案2】:

我找到了解决办法:

     Iterator<String> keys = response.keys();
     while (keys.hasNext()) 
            String keyValue = keys.next();
            String valueString = response.getString(keyValue);

【讨论】:

以上是关于如何获取根据 JAVA 中的用户选择而变化的 JsonObjects?的主要内容,如果未能解决你的问题,请参考以下文章

easyui里面如何实现选中下拉框中的内容然后其他栏随着下拉框中内容变化而变化

js如何获取css文件内的一个属性值

js动态获取下拉框的数据(搜索条件之间互相影响)

js获取表格中的数据 以及 表格中checkbox选中一行数据

如何从 Java 中的 Oracle SQL 选择中获取原始脚本输出而不是查询结果

根据国家/地区获取货币符号