androidjsonapijson-apifixer.io

I have a problem to showing currency rates in Textview in android studio


i am unable to show currency rates in text view i want to show currency rate then i have to calculate its value in Saudi riyal and Arab emirates Durham i am using only 11 currencies

i am solving it from 3 days but its not working in any way

   import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;

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

import java.util.Currency;


public class currencconvert extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

    private static final String TAG = "tag";
    //Url
    //this Prayer Timings is only for london now

    // Tag used to cancel the request
    String tag_json_obj = "json_obj_req";

    //progress Dialog
    ProgressDialog pDialog;

    TextView usdtxt, eurtxt, inrtxt, pkrtxt, afntxt, bdttxt, idrtxt, myrtxt, trytxt, aedtxt, sartxt;
    EditText addnum;
    Button cnvrtbtn;



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

        usdtxt      =  findViewById(R.id.usd);
        eurtxt      =  findViewById(R.id.euro);
        inrtxt     =   findViewById(R.id.inr);
        pkrtxt     = findViewById(R.id.pkr);
        afntxt   =     findViewById(R.id.afn);
        bdttxt      =  findViewById(R.id.bdt);
        idrtxt      =  findViewById(R.id.idr);
        myrtxt      =  findViewById(R.id.myr);
        trytxt      =  findViewById(R.id.tury);
        aedtxt      =  findViewById(R.id.aed);
        sartxt      =  findViewById(R.id.sar);

       Spinner spinner = (Spinner) findViewById(R.id.spin);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency, android.R.layout.simple_gallery_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

       // link wagera he ye


        // function to get location
        Currencconvert();

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
    }

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

    }



    public void Currencconvert() {

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();



        String Url  = "http://data.fixer.io/api/latest?access_key=9ec08275542de6b1646a986ffe785667";


        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, Url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        try {

                            Toast.makeText(currencconvert.this, "your code run", Toast.LENGTH_SHORT).show();

                            // get date

                           //String date = response.getJSONArray("JSON").getJSONObject(0).get("date").toString();


                            // for getting namaz timings
                            String usdtx = response.getJSONArray("rates").getJSONObject(0).get("USD").toString();
                            String eurtx = response.getJSONArray("rates").getJSONObject(0).get("EUR").toString();
                            int inrtx =  Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("INR").toString());
                            int pkrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("PKR").toString());
                            int afntx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("AFN").toString());
                            int bdttx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("BDT").toString());
                            int idrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("IDR").toString());
                            int myrtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("MYR").toString());
                            int trytx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("TRY").toString());
                            int aedtx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("AED").toString());
                            int sartx = Integer.parseInt(response.getJSONArray("rates").getJSONObject(0).get("SAR").toString());

                            Toast.makeText(currencconvert.this, "your code in  the middle", Toast.LENGTH_SHORT).show();

                            usdtxt.setText(usdtx);
                            eurtxt.setText(eurtx);
                            inrtxt.setText(inrtx);
                            pkrtxt.setText(pkrtx);
                            afntxt.setText(afntx);
                            bdttxt.setText(bdttx);
                            idrtxt.setText(idrtx);
                            myrtxt.setText(myrtx);
                            trytxt.setText(trytx);
                            aedtxt.setText(aedtx);
                            sartxt.setText(sartx);


                            Toast.makeText(currencconvert.this, "congrats your code run completend", Toast.LENGTH_SHORT).show();


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        pDialog.hide();
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(currencconvert.this, "Error", Toast.LENGTH_SHORT).show();

                pDialog.hide();
            }
        });

// Adding request to request queue
        AppController.getInstance().addToRequestQueue(request, tag_json_obj);

    }
}

Solution

  • If the parameter of the setText method is an integer value, it will be taken as the resource identifier of the string resource to be displayed. So you should pass CharSequence instead of an integer. So your code should be-

    usdtxt.setText(String.valueOf(inrtx));
    eurtxt.setText(String.valueOf(pkrtx));
    ...