-2

i'm tyring to get parse json in int to textview in xml but dont know how.

this is the json

{
   "sys":
   {
      "sunrise":1381107633,
      "sunset":1381149604
   }
}

i'm tyring to call the textview and change it to int but still didn't work.

TextView asetBaik = (TextView) findViewById(R.id.asetangka);

and this is how i call the json

@Override
        protected String doInBackground(String... params) {
            String link_url = "https://example.com/api/sun";
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.AmbilJson(link_url);

                try {
                    JSONObject data = json.getJSONObject("sys");

                    baik = Integer.parseInt(asetBaik.getText().toString());
                    baik = data.getInt("sunrise");
                    asetBaik.setText(baik);

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

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
        }

    }

please help me how i can take the int value json and take it in Sting TextView

Qube
  • 543
  • 3
  • 9
  • 23

4 Answers4

1

you are updating the text view in the different thread(not the UI thread). you can update the view in two ways:

  1. update the text view by running it on the ui thread. ref- https://developer.android.com/reference/android/app/Activity#runonuithread
  2. You can return the result in doInBackground and update the text view in onPostExecute
Dk3492
  • 11
  • 1
1

There is a only one Thread running which is UI Main Thread, and it doesn't allowed to update UI from thread process.

So there is a solution if you want to set the TextView inside the doInBackground() method, do the UI updating operations inside the runOnUiThread method.

runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   try {
                    JSONObject data = json.getJSONObject("sys");

                    baik = Integer.parseInt(asetBaik.getText().toString());
                    baik = data.getInt("sunrise");
                    asetBaik.setText(baik);

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

Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

replace this code

baik = Integer.parseInt(asetBaik.getText().toString());
baik = data.getInt("sunrise");
asetBaik.setText(baik);

to

baik = data.getInt("sunrise");
asetBaik.setText(String.valueOf(baik));

Hope this will work

Md Jubayer
  • 272
  • 3
  • 13
0

I Hope this will work for you.

 try {

         JSONObject jsonObject1= json.getJSONObject("sys");
         int sunrise = jsonObject1.getInt("sunrise");
         int sunset = jsonObject1.getInt("sunset");
         Log.e("sunrise", "" + sunrise);
         Log.e("sunset", "" + sunset);

       } catch (Exception e) {

   }

you are using asynctask you need to use handler to update the UI on main thread

GParekar
  • 1,209
  • 1
  • 8
  • 15