0
package com.sp.demoapiheader;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

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

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private TextView mTextViewResult;
    private RequestQueue mQueue;

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

        mTextViewResult = findViewById(R.id.text_view_result);
        Button buttonParse = findViewById(R.id.button_parse);

        StringRequest request = new StringRequest(Request.Method.GET, "http://datamall2.mytransport.sg/ltaodataservice/BusServices", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (!response.equals(null)) {
                    Log.e("Your Array Response", response);

                } else {
                    Log.e("Your Array Response", "Data Null");
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("error is ", "" + error);
            }
        }) {

            //This is for Headers If You Needed
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json; charset=UTF-8");
                params.put("AccountKey", "XXXX==");
                return params;
            }

            //Pass Your Parameters here
            /*@Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("User", UserName);
                params.put("Pass", PassWord);
                return params;
            }*/
        };
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        queue.add(request);

    }
}

I would like to parse the data into a textview and display each variable in a string/int format. As can be seen in the code, there's an simple XML file with a button and textview. Do i implement JSONArray or JSONObject at some point into the code? Currently, the code only allows a response in the run tab.

2
  • did you try this? stackoverflow.com/questions/44626934/… Commented Feb 4, 2020 at 5:29
  • @ManikandanK, Hi sir, yes i'm able to get my string data response to show in the run view, but what i would like to achieve is to get it show on a TextView instead Commented Feb 4, 2020 at 5:40

1 Answer 1

0

You can achieve json array parser for two ways to shows in textview

1) Gson convertor dependency

dependencies {
  implementation 'com.google.code.gson:gson:2.8.6'
}


@Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.d(TAG, "onResponse: " + response.body());
        List<MenuResponseModel> listArr = new ArrayList<>();
            Gson gson = new Gson();
            StringBuffer stringBuffer = new StringBuffer();
            try {               
                MenuResponseModel menuResponseModel = gson.fromJson(response.body().string(), MenuResponseModel.class);
                if (menuResponseModel.getStaus().equals("1")) {
                    listArr.addAll(menuResponseModel.getData());                        
                 for (int i = 0; i < listArr.size(); i++) {

                    Integer price = Integer.parseInt(foodOrderList.get(i).getFood_quantity()) * Integer.parseInt(foodOrderList.get(i).getFood_price());
                    stringBuffer.append("\n\nFood Name: " + foodOrderList.get(i).getFood_name() + "\n Food Quantity : " + foodOrderList.get(i).getFood_quantity() + "\n Food Price :" + price.toString());
                tv_order_details.setText(stringBuffer.toString());
            }

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

2) JSONArray classs

try {
                JSONObject jsonObject = new JSONObject(response.body().string());
                JSONArray jsonArray = jsonObject.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject js = jsonArray.getJSONObject(i);
                    stringBuffer.append("Name :"+js.getString("customer_name")+" - Table No: "+js.getString("table_id")+js.getString("seats"));
                }   mTextViewResult.setText(stringBuffer.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.