Updated Question from previous: I filled an array through HashMap, Iam using Asynctask for http request & after filling array put that array in dialog box. When I first run my app it gives me an empty dialog box & didn't give any error but when I re run my app it shows all array elements in dialog box perfectly. Whats the reason ?
//JsonResponse Inner Class in main class
private class JsonResponse extends AsyncTask<String, Void, String>
{
String response = "";
private ArrayList<HashMap<String, String>> prServices_resultList =
new ArrayList<HashMap<String, String>>();
protected void onPostExecute(String result)
{
if(response.equalsIgnoreCase("Success"))
{
ResultList_List = prServices_resultList;
int s=0;
for (HashMap<String, String> hashServices : prServices_resultList)
{
Db_Services[s] = hashServices.get(android_S_CName);
Db_ServicesID[s] = hashServices.get(android_S_ID);
s++;
}
}
}
protected String doInBackground(final String... args)
{
JSONParser jParser = new JSONParser();
JSONArray jArrayServices = jParser.getJSONFromUrl(url_Services);
try
{
for (int i = 0; i < jArrayServices.length(); i++)
{
JSONObject jsonElements = jArrayServices.getJSONObject(i);
String S_id = jsonElements.getString(android_S_ID);
String S_name = jsonElements.getString(android_S_NAME);
HashMap<String, String> hashServices = new HashMap<String, String>();
// adding each child node to HashMap key
hashServices.put(android_S_ID, S_id);
hashServices.put(android_S_NAME, S_name);
// adding HashList to ArrayList
prServices_resultList.add(hashServices);
}
response = "Success";
}
catch(JSONException e)
{
e.printStackTrace();
}
return response;
}
}
In my main class have have a button & when i press i execute AsyncTask:
new JsonResponse().execute;
In main class above onCreate i declare like:
static ArrayList<HashMap<String, String>> ResultList_Services =
new ArrayList<HashMap<String, String>>();
String[] Db_Services = new String[ResultList_Services.size()];
String[] Db_ServicesID = new String[ResultList_Services.size()];
You are creating an empty map here:
ResultList_Services = new ArrayList<HashMap<String, String>>();
Then trying to initialize two arrays with the size of an empty map - being zero.
// ResultList_Services.size() will be zero
String[] Db_Services = new String[ResultList_Services.size()];
String[] Db_ServicesID = new String[ResultList_Services.size()];
So when you try adding to these arrays it will throw an OutOfBoundsException
You could make these Arrays into lists, then you can dynamically add elements as needed without needing to specify a size to start with. If you then need an Array (for other Methods) you can get an array from a list using List#toArray()
As per your comment
You could just create temporary arrays to which you add all the elements and then assign this to your other arrays, something like
protected void onPostExecute(String result)
{
if(response.equalsIgnoreCase("Success"))
{
ResultList_List = prServices_resultList;
String[] tmp_dbServ = new String[prServices_resultList.size()];
String[] tmp_dbServID = new String[prServices_resultList.size()];
int s=0;
for (HashMap<String, String> hashServices : prServices_resultList)
{
tmp_dbServ[s] = hashServices.get(android_S_CName);
tmp_dbServID[s] = hashServices.get(android_S_ID);
s++;
}
Db_Services = tmp_dbServ;
Db_ServicesID = tmp_dbServID;
}
}
Related
I have a hashmap of string
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name", name);
hashMap.put("number", number);
hashMap.put("profileImage", image);
and a List of Hashmap
List<HashMap<String, String>> recents = new ArrayList<>();
recents.add(hashMap);
Now I have to save this list
I have tried using https://github.com/pilgr/Paper to save this List
Paper.book().write("recents", recents);
but i can't get the list back
List<HashMap<String, String>> list = Paper.book().read("recents");
HashMap<String,String> hashMap = list.get(0);
String name = hashMap.get("name");
String number = hashMap.get("number");
String image = hashMap.get("profileImage");
Uses of the code
actually I'm passing this list to recycelerViewAdapeter and from there in OnBindViewHolder() I'm getting all the Hashmap values and displaying it to user
Saving Data Code
Paper.init(this);
List<HashMap<String, String>> recents = new ArrayList<>();
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name", name);
hashMap.put("number", number);
hashMap.put("profileImage", image);
recents.add(hashMap);
Paper.book().write("recents", contacts);
Receiving Data Code
Paper.init(requireActivity());
recyclerView = view.findViewById(R.id.recyclerView);
List<HashMap<String, String>> list = Paper.book().read("recents");
Adapter = new Adapter(requireActivity(), list);
recyclerView.setAdapter(Adapter);
You can use Gson and SharedPreferences to do the same.
implementation 'com.google.code.gson:gson:2.8.9'
Example:
private SharedPreferences sp;
private HashMap<String, Boolean> favs;
....
....
public void addFavourite(String wall_name) {
favs = getFavourites();
favs.put(wall_name, true);
setFavourties();
}
public void removeFav(String wall_name) {
favs = getFavourites();
favs.put(wall_name, false);
setFavourties();
}
private void setFavourties() {
SharedPreferences.Editor pe = sp.edit();
Gson gson = new Gson();
String j = gson.toJson(favs);
pe.putString("Favourites", j);
pe.apply();
}
public HashMap<String, Boolean> getFavourites() {
Gson gson = new Gson();
String j = sp.getString("Favourites", null);
if (j != null) {
Type stringBooleanMap = new TypeToken<HashMap<String, Boolean>>() {
}.getType();
return gson.fromJson(j, stringBooleanMap);
} else {
return new HashMap<>();
}
}
I want to put data from a MySQL database table into Android Studio.
For modifying script from tutorial.
In tutorial ArrayList value is:
HomeCollection.date_collection_arr = new ArrayList<HomeCollection>();
HomeCollection.date_collection_arr.add(new HomeCollection("2018-07-08" ,"Title1","Subject1","Description1"));
HomeCollection.date_collection_arr.add(new HomeCollection("2018-07-09" ,"Title2","Subject2","Description2"));
HomeCollection.date_collection_arr.add(new HomeCollection("2018-07-10" ,"Title3","Subject3","Description3"));
My Question:
How to modify data Array, in example with all data in MySql table
having below columns?
| id | date | title | subject | description |
Any help will be greatly appreciated
//you can do like this.
get the value from arraylist into string array then split the array and then send the string to database.given below code
String[] value = HomeCollection.date_collection_arr.get(0).split(",");
String date=value[0];
String title=value[1];
String description=value[2];
//if your array list value contain space use below code
String[] valuetrim= HomeCollection.date_collection_arr.get(0).split("\\s*,\\s*");
String valuetrim=value[0];
String valuetrim=value[1];
String valuetrim=value[2];
Thanx for your clue, but I still not understand,
I try to get array list, from this way n success to views data at list (bellow array scrip),
maybe someone can give me more specific, how to implement/combine value data i have got from this way (below) to array at my question (i want to impemented sample script from this link https://www.developerbrothers.com/highlight-events-custom-calendar-android-studio-developerbrothers-com/) but i want data values get from MySql, Thanks
private void showOrder(){
JSONObject jsonObject = null;
ArrayList> list = new ArrayList>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String idplace = jo.getString(konfigurasi.TAG_PLACE);
String titl = jo.getString(konfigurasi.TAG_TITLE);
String subj = jo.getString(konfigurasi.TAG_SUBJECT);
String desc= jo.getString(konfigurasi.TAG_DESCRIPTION);
String dateo = jo.getString(konfigurasi.TAG_DATEORDER);
HashMap<String,String> orders = new HashMap<>();
orders.put(konfigurasi.TAG_PLACE,idplace);
orders.put(konfigurasi.TAG_TITLE,titl);
orders.put(konfigurasi.TAG_SUBJECT,subj);
orders.put(konfigurasi.TAG_DESCRIPTION,desc);
orders.put(konfigurasi.TAG_DATEORDER,dateo);
list.add(orders);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
DateEventActivity.this, list, R.layout.list_item,
new String[]{konfigurasi.TAG_PLACE,konfigurasi.TAG_TITLE,konfigurasi.TAG_SUBJECT,konfigurasi.TAG_DESCRIPTION,konfigurasi.TAG_DATEORDER},
new int[]{R.id.idplace, R.id.titl, R.id.subj, R.id.desc, R.id.dateo});
listView.setAdapter(adapter);
}
private void getJSON(){
class GetJSON extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(DateEventActivity.this,"Mengambil Data","Mohon Tunggu...",false,false);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
JSON_STRING = s;
showOrder();
}
#Override
protected String doInBackground(Void... params) {
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
return s;
}
}
GetJSON gj = new GetJSON();
gj.execute();
}
I am trying to pass string and int data (possibly other data types like time) to a HashMap to use in a doinbackground task in Android to amend a URL. The URL uses key value pairs to update a mysql database.
I've read about using an object to pass multiple variable types, but can't get it to work.
private void addChore(){
final String title2 = editTextTaskTitle.getText().toString().trim();
final String description2 = editTextDescription.getText().toString().trim();
final String person2 = itemPerson.toString().trim();
final int monday2 = cbMon;
class NewChore1 {
String title1;
String description1;
String person1;
int monday1;
NewChore1(String title1, String description1, String person1, int monday1){
this.title1 = title1;
this.description1 = description1;
this.person1 = person1;
this.monday1 = monday1;
}
}
class AddChoreM extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(AddChore.this,"Adding...","Wait...",false,false);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(AddChore.this,s,Toast.LENGTH_LONG).show();
}
#Override
protected String doInBackground(Void... v) {
HashMap<String, NewChore1> params1 = new HashMap<>();
params1.put(Config.KEY_CHORE_TASK_TITLE,?);
params1.put(Config.KEY_CHORE_DESCRIPTION,?);
params1.put(Config.KEY_CHORE_PERSON,?);
params1.put(Config.KEY_CHORE_MONDAY,?);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.URL_ADD, params1);
return res;
}
}
NewChore1 params = new NewChore1(title2, description2, person2, monday2);
AddChoreM addChoreM = new AddChoreM();
addChoreM.execute(params);
}
In RequestHandler, I have used the following.
private String getPostDataString(HashMap<String, Object> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
Edit: I was not quick enough so there are other answers already but the changes below should work. You can pass your NewChore1 object to your task and extract the parameters in doInBackground:
class AddChoreM extends AsyncTask<NewChore1,Void,String> {
And:
#Override
protected String doInBackground(NewChore1...chore) {
HashMap<String, String> params1 = new HashMap<>();
params1.put(Config.KEY_CHORE_TASK_TITLE, chore[0].title1);
params1.put(Config.KEY_CHORE_DESCRIPTION, chore[0].description1);
params1.put(Config.KEY_CHORE_PERSON,chore[0].person1);
params1.put(Config.KEY_CHORE_MONDAY,chore[0].monday1);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.URL_ADD, params1);
return res;
}
Finally:
NewChore1 params = new NewChore1(title2, description2, person2, monday2);
new addChoreM.execute(params);
Update:
Since sendPostRequest only accepts HashMap<String, String> you need to change to: HashMap<String, String> params1 = new HashMap<>();
And change your NewChore1 class to take only Strings.
If you use
Map<String, Object> params1 = new HashMap<>();
then you can store any type as value within the map.
Create a constructor for AddChoreM class and set your NewChore1 object through it. You can now easily extract the properties of NewChore1 in doInBackground.
class AddChoreM extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
NewChore1 newChore1Obj;
public AddChoreM(NewChore1 newChore1Obj){
this.newChore1Obj = newChore1Obj;
}
#Override
protected String doInBackground(Void...v) {
HashMap<String, NewChore1> params1 = new HashMap<>();
String res = "";
if(newChore1Obj != null) {
params1.put(Config.KEY_CHORE_TASK_TITLE, newChore1Obj.title1);
params1.put(Config.KEY_CHORE_DESCRIPTION, newChore1Obj.description1);
params1.put(Config.KEY_CHORE_PERSON,newChore1Obj.person1);
params1.put(Config.KEY_CHORE_MONDAY,newChore1Obj.monday1);
RequestHandler rh = new RequestHandler();
res = rh.sendPostRequest(Config.URL_ADD, params1);
}
return res;
}
// Other methods of AsyncTask
//
}
Finally, create and execute AddChoreM like this.
NewChore1 params = new NewChore1(title2, description2, person2, monday2);
AddChoreM addChoreM = new AddChoreM(params);
addChoreM.execute();
I am trying to get values from a json data with AsyncTask. I am getting only the last value and I don't understand why...
I tryed to parse with for each, while but I am doing something wrong :
Here is my code :
private class DecodeData extends AsyncTask<String, Void, String> {
protected ArrayList<HashMap<String, String>> decodedArray;
protected HashMap<String, String> decodedMap;
protected Iterator<String> it;
protected JSONArray m_Array;
protected JSONObject object;
protected String response;
protected String keys;
protected String value;
#SuppressWarnings("unchecked")
#Override
protected String doInBackground(String... params) {
response = params[0];
keys = "";
value = "";
object = null;
decodedArray = new ArrayList<HashMap<String, String>>();
try {
JSONArray arrayResp = new JSONArray(response);
for (int i = 0; i < arrayResp.length(); i++) {
decodedMap = new HashMap<String, String>();
it = arrayResp.getJSONObject(i).keys();
while (it.hasNext()) {
keys = (String)it.next();
value = Base64.DecodeStrToStr((String)arrayResp.getJSONObject(i).get(keys));
decodedMap.put("\""+keys+"\"", "\""+value+"\"");
object = new JSONObject(decodedMap.toString());
Log.i("DECODED MAP : ", object.toString());
m_Array = new JSONArray();
m_Array.put(object);
Log.i("M_ARRAY", ""+m_Array);
}
// decodedArray.add(decodedMap);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// array = new JSONArray(decodedArray);
return m_Array.toString();
}
I am using Volley to get response. After that, I create a JSONArray with this response and I get all keys/values from it. I put all of them in my Hashmap. But when i'm putting keys/values here : m_Array.put(object), it puts only the last value of my json data. Anybody has an idea of what I'm making wrong ?
Please create JSONArray before starting for loop..
m_Array = new JSONArray();
JSONArray arrayResp = new JSONArray(response);
for (int i = 0; i < arrayResp.length(); i++) { ....
Updated: I filled an array through HashMap, Iam using Asynctask for http request & after filling array put that array in dialog box. When I first run my app it gives me an empty dialog box & didn't give any error but when I re run my app it shows all array elements in dialog box perfectly. Whats the reason ?
//JsonResponse Inner Class in main class
private class JsonResponse extends AsyncTask<String, Void, String> {
String response = "";
private ArrayList<HashMap<String, String>> prServices_resultList = new ArrayList<HashMap<String, String>>();
protected void onPreExecute()
{
}
protected void onPostExecute(String result)
{
if(response.equalsIgnoreCase("Success"))
{
ResultList_List = prServices_resultList;
int z=0;
for (HashMap<String, String> hashList : prServices_resultList)
{
Av_List[z] = hashList.get(android_Av_ID);
Av_Lat[z] = Double.parseDouble(hashList.get(android_Av_LAT));
Av_Lng[z] = Double.parseDouble(hashList.get(android_Av_LONG));
z++;
}
}
}
protected String doInBackground(final String... args)
{
JSONParser jParser = new JSONParser();
JSONArray jArrayServices = jParser.getJSONFromUrl(url_Services);
try{
for (int i = 0; i < jArrayServices.length(); i++)
{
JSONObject jsonElements = jArrayServices.getJSONObject(i);
String S_id = jsonElements.getString(android_S_ID);
String S_name = jsonElements.getString(android_S_NAME);
HashMap<String, String> hashServices = new HashMap<String, String>();
// adding each child node to HashMap key
hashServices.put(android_S_ID, S_id);
hashServices.put(android_S_NAME, S_name);
// adding HashList to ArrayList
prServices_resultList.add(hashServices);
}
response = "Success";
}
catch(JSONException e)
{
e.printStackTrace();
}
return response;
}
}
In my main class when i press a button:
new JsonResponse().execute;
In main class above onCreate i declare like:
static ArrayList<HashMap<String, String>> ResultList_Services = new ArrayList<HashMap<String, String>>();
String[] Db_Services = new String[ResultList_Services.size()];
String[] Db_ServicesID = new String[ResultList_Services.size()];
Now I get an error: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
First of all, adding values to static field ResultList_Services accessible for UI-thread from the background thread is very bad practice. You should rewrite your code to be thread-safe. The are several options, here is the one:
private class JsonResponse extends AsyncTask<String, Void, String> {
private ArrayList<HashMap<String, String>> resultList = new ArrayList<HashMap<String, String>>();
...
protected String doInBackground(final String... args)
{
...
// adding HashList to private JsonResponse's field
resultList.add(hashServices);
...
}
protected void onPostExecute(String result)
{
if (response.equalsIgnoreCase("Success"))
{
ResultList_Services = resultList;
//TODO: notify your Activity/Dialog on completion
}
}
}
Concerning your question - the reason for not seeing new records, is that you show the dialog when there are no values in the ResultList. You should request to show it from onPostExecute, for example.
You need to do this -
for (HashMap<String, String> hashServices : ResultList_Services)
{
Db_Services[s] = hashServices.get(android_S_CName);
Db_ServicesID[s] = hashServices.get(android_S_ID);
s++;
}
in onPostExcecute() block of your asynctask.