Android - java adding please wait message - android

My question is how can i add a message "Loading please wait " . Until the request is made and response is parsed to table.
Is there approach to that using this method ?
Like this when i run this i don't know if there is no data on response or response is not yet received.
StringRequest postRequest = new StringRequest(Request.Method.POST, apiURL,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray jsonArray = jsonObj.getJSONArray("sales");
for (int i=0;i<jsonArray.length();i++){
JSONObject employee = jsonArray.getJSONObject(i);
TableRow tbrow = new TableRow( MainActivity.this );
TextView t1v = new TextView(MainActivity.this);
t1v.setText(employee.getString("saletype"));
t1v.setTextColor(Color.BLACK);
stk.addView(tbrow);
// .......
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
}
}
) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
params.put("store", spinner.getSelectedItem().toString());
return params;
}
};
mQueue.add(postRequest);

Show Progress bar on adding request object in queue.
mQueue.add(postrequest);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading ...Please Wait");
progressDialog.show();
Then Dismiss it on receiving response.
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray jsonArray = jsonObj.getJSONArray("sales");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject employee = jsonArray.getJSONObject(i);
TableRow tbrow = new TableRow(MainActivity.this);
TextView t1v = new TextView(MainActivity.this);
t1v.setText(employee.getString("saletype"));
t1v.setTextColor(Color.BLACK);
stk.addView(tbrow);
// .......
}
progressDialog.dismiss();
} catch (Exception e) {
progressDialog.dismiss();
}
Also,
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
}

Related

Textview not showing anything from JSON Object

Response from android is good which is the database i choose based on some value
Response: {"users":[{"id":"6","unique_id":"5f8fb2d3ee71c9.27038026","name":"siapa","email":"aku","encrypted_password":"G2HXPazvWEKRdm6lo5IDs4KSDEQ5ZTdlYjgyODBj","salt":"9e7eb8280c","created_at":"2020-10-21 11:02:27","updated_at":"2020-10-21 20:24:19"}]}
i need to show only object "updated_at" in textview but didnt show anything
this is my json code
private void getData() {
String tag_string_req = "get";
ArrayList<HashMap<String, String>> list_data;
list_data = new ArrayList<HashMap<String, String>>();
StringRequest strReq = new StringRequest(Method.GET,
AppConfig.URL_GET, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("users");
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject json = jsonArray.getJSONObject(a);
HashMap<String, String> map = new HashMap<String, String>();
map.put("updated_at", json.getString("updated_at"));
list_data.add(map);
}
update_time.setText(list_data.get(0).get("updated_at"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
});
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
what could be wrong?

Not able to fetch JSON data from api of covid19india for my android app using Volley

I want to fetch data from this file: https://api.covid19india.org/state_district_wise.json
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading data...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
"https://api.covid19india.org/state_district_wise.json",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("Jammu and Kashmir");
progressDialog.cancel();
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// I want get case details of "Jammu and Kashmir"
}
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
What should I use here:
JSONArray jsonArray = jsonObject.getJSONArray("Jammu and Kashmir");
Data comming from API is JSONArray, not JSONObject
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, "https://api.covid19india.org/v2/state_district_wise.json",null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject dataOBJ = response.getJSONObject(i);
JSONArray jsonChild = dataOBJ.getJSONArray("districtData");
for (int k = 0; k < jsonChild.length(); k++) {
JSONObject obj = jsonChild.getJSONObject(k);
// work to be done...
}
}
} catch (Exception exp) {
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
The code is working fine now. Read this for complete sollution.

How do I return a Hashmap inside a JSONArray response?

I want to access email and phone in HashMap from outside of the JSONArray Response. How can I do this?
I tried making globale variables and assign values to them. It didn't worked.
JsonArrayRequest request1 = new JsonArrayRequest(Request.Method.GET,
url,
null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
List<HashMap<String, String>> list = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
HashMap<String, String> map = new HashMap();
map.put("email", obj.getString("email"));
map.put("phone", obj.getString("phone"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(HomeActivity.this, "Error"+error, Toast.LENGTH_SHORT).show();
}
});
Declare HashMap<String, String> map = new HashMap(); at top level in your class
And use it in your JsonArrayRequest
#Override
public void onResponse(JSONArray response) {
List<HashMap<String, String>> list = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
map.put("email", obj.getString("email"));
map.put("phone", obj.getString("phone"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

How to update json data using volley method on Android?

I get the error no value for city.
I'm new to Android. I was unable to pass the spinner data using PHP URL, volley method. And in my XML I'm using the spinner and text views city, id and pass the city list through spinner URL.
Here is my activity:
private void spinnerapi(){
sprCoun = (Spinner) findViewById(R.id.spinner);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String serverURL = "server url";
final StringRequest getRequest = new StringRequest(Request.Method.POST, serverURL,
new com.android.volley.Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("officerResponse", response);
try {
JSONObject jsonObject = new JSONObject(response);
String str_status = jsonObject.getString("status");
String str_message = jsonObject.getString("message");
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObjectGuest = jsonArray.getJSONObject(i);
city_name = jsonObjectGuest.getString("cityname");
city_id = jsonObjectGuest.getString("cityid");
listarraylist.add(new CityModel(city_name));
}
sprCoun.setAdapter(new ArrayAdapter<String>
(RegistrationActivity.this, android.R.layout.simple_spinner_dropdown_item, list));
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "" + e, Toast.LENGTH_LONG).show();
}
}
},
new com.android.volley.Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// error
// Toast.makeText(context, "" + error, Toast.LENGTH_LONG).show();
Log.d("tyghj", String.valueOf(error));
}
}
) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
queue.add(getRequest);
}

Android http request synchron with Volley

I write code to make a request http to server:
public void reqCategory() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://myaddress/getdata.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Json Parse
try {
JSONArray jsonArray = new JSONArray(response);
catList.clear();
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
catList.add(jsonObject.getString("categoria"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error -> ", error.toString());
}
}) {
#Override
protected Map<String, String> getParams() { //nessun parametro
Map<String, String> params = new HashMap<String, String>();
//no param at moment
return params;
}
};
queue.add(postRequest);
}
I declare:
List<String> catList = new ArrayList<String>();
in my onCreate I have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comanda);
reqCategory();
for (String s : catList) {
Log.d("Categoria -> ", s);
}
}
This should be fill the List catList with values from reqCategory(), but the for loop doesnt show the values, because the request is asyncron.
How I can make a syncron request so I have the values in catList after the reqCategory() call and I can see the values from loop for.
Thank you.
try some minor edit in your code
for (int i=0; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String Temp = new
catList.add(new String(jsonObject.getString("categoria")));
}
You cannot have networking blocking UI thread on Android. All networking must be on separate thread, therefore you need to refactor your code to be like, i.e.:
onCreate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comanda);
reqCategory();
}
where reqCategory() must spawn i.e. AsyncTask for the networking. Once AsyncTask is done you can executre your loop in its onPostExecute() (or in odInBackground() if you do not need to touch UI widgets).

Categories

Resources