dubbel json output everytime switching activities - java

the problem that i'm facing right now is;
on my homeactivity i have some outputs from json. but those values does change so i have to keep homeactivity updated.
when i add jsonrequest() in the onRestart()
it does get the latest values when i restart the app, or when i'm switching between activities. BUT everytime that i'm switching between activities it does add more fields. example: in my json file i do have
username: John
homeactivity shows John ; switching between activities, the homeactivity shows; John John etc..
i also have jsonrequest() in the onCreate()
private void jsonrequest() {
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0 ; i < response.length(); i++ ) {
try {
jsonObject = response.getJSONObject(i);
User user = new User();
user.setUsername(jsonObject.getString("username"));
lstUser.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(lstUser);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(DashBoardActivity.this);
requestQueue.add(request) ;
}

value in lstUser gets double every time because you are adding new value in lstUser every time, but you are not removing old value from that array.
so before adding User in lstUser, remove old value available in that array. As :
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
lstUser.clear(); // Here remove old value, so in new list you have only latest values.
for (int i = 0 ; i < response.length(); i++ ) {
try {
jsonObject = response.getJSONObject(i);
User user = new User();
user.setUsername(jsonObject.getString("username"));
lstUser.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(lstUser);
}

Related

ArrayList doesnt return elements when changing fragments after get request from api

I have a method that call a api and fill a ArrayList with data. The method is in a fragment, when I open the App it adds data to the arrayList, but when I go to another fragment or activity and come back it doesnt show any in the recyclerview from the ArrayList as if nothing is there, but when I toast the size of array, it returns the number of elements that should be there
but it doesnt show/populate the recyclerview with the elements.
I call the method in onCreateView()
CALLING API
private void getDataFromUAPI(String gist_json_url){
requestQueue = MySingleton.getInstance(this.getContext()).getRequestQueue();
request = new JsonArrayRequest(Method.GET, gist_json_url, null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i = 0; i < response.length(); i ++){
try {
jsonObject = response.getJSONObject(i);
User user = new User();
// if this user Id is not user Id of the user and
// if user are already been followed, than don't add
if (!jsonObject.getString("id").equals(firebaseUser.getUid()) && !sharedPreferences.contains(jsonObject.getString("id"))) {
user.setPhoto(jsonObject.getString("photo"));
user.setName(jsonObject.getString("name"));
user.setFnumber(jsonObject.getString("fnumber"));
user.setId(jsonObject.getString("id"));
responseList.add(user);
}
//Toast.makeText(getContext(), user.getId(), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
Toast.makeText(getContext(), String.valueOf(responseList.size()), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "ERROR \n" + error.toString(), Toast.LENGTH_LONG).show();
}
});
requestQueue.add(request);

Populating spinner with custom object .how to set spinner hint in custom object

I am developing one application where one spinner are there I am parsing JSON in the spinner and setting an adapter with the custom object. now I am getting an issue unable to set hint . as in image first country coming to Afghanistan but it should come like " Select Country " as a hint. please help me I am stuck in this from 2 days. thanks in advance.
List mCountryList ;
private void getCountry() {
ApiService countryApi = RetrofitClient.getApiService();
Call<JsonElement> call = countryApi.getCountry();
mCountryList = new ArrayList<>();
call.enqueue(new Callback<JsonElement>() {
#Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
if (response.isSuccessful()) {
try {
JSONObject object = new JSONObject(response.body().toString());
JSONObject jsonObj = new JSONObject(object.toString());
JSONArray jsonArray = jsonObj.getJSONArray("data");
if (jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Country countryList = new Country();
countryList.setId(jsonObject.getInt("id"));
countryList.setName(jsonObject.getString("name"));
countryList.setCode(jsonObject.getString("code"));
mCountryList.add(countryList);
}
ArrayAdapter<Country> arrayAdapter = new ArrayAdapter<Country>(getApplicationContext(),R.layout.spinner_layout, mCountryList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerCountry.setAdapter(arrayAdapter);
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (response.code() == 401 || response.code() == 500) {
Toast.makeText(CreateAccountActivity.this, "Session Expired", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CreateAccountActivity.this, "Something Went Wrong..!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Toast.makeText(CreateAccountActivity.this, "Something Went Wrong..!", Toast.LENGTH_SHORT).show();
}
});
}[enter image description here][1]
One easy way is to create a dummy Country object at beginning and add to array whose name is Select Country and fill other dummy details which you might be needing and while tapping on any form button (assuming you have one) you can check the current selected index and if it is 0 then no country is selected. In that case you can show a Toast.
For example here is your updated try catch block:
try {
JSONObject object = new JSONObject(response.body().toString());
JSONObject jsonObj = new JSONObject(object.toString());
JSONArray jsonArray = jsonObj.getJSONArray("data");
if (jsonArray.length() > 0) {
Country country = new Country();
country.setId(0);
country.setName("Select Country");
country.setCode(0);
mCountryList.add(country);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Country country = new Country();
country.setId(jsonObject.getInt("id"));
country.setName(jsonObject.getString("name"));
country.setCode(jsonObject.getString("code"));
mCountryList.add(country);
}
ArrayAdapter < Country > arrayAdapter = new ArrayAdapter < Country > (getApplicationContext(), R.layout.spinner_layout, country);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerCountry.setAdapter(arrayAdapter);
}
} catch (Exception e) {
e.printStackTrace();
}
1st element is your hint and make sure you check the index when you are actually want to take any action i.e. if index is 0 show user a Toast to select a Country.

Android Studio - Parsing JSON from Google Places Details API Phone Number giving null on first run

Bit of a strange issue here. Currently I have an activity which gets the names and place ids of all the taxi companies using Google Places Search API and places them into a list view. Then when the item is clicked it will take the place id and run the Places Details API to fetch the phone number.
Currently I am running into the issue where the first time the item is clicked in the list view the JSON parser will return a NULL and the phone will dial a number like 6855 or 685-5. If you go back and click on the same item or another item then the phone number will fetch correctly.
In the activity I am running 2 JSON parsers however they run at different times.
onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taxi);
mQueue = Volley.newRequestQueue(this);
pQueue = Volley.newRequestQueue(this);
listItem = new ArrayList<>();
placeid = new ArrayList<>();
police = findViewById(R.id.textView11);
userlist = findViewById(R.id.users_list);
jsonParse();
userlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String phone = jsonParseNumber(position);
}
});
}
jsonParse (Gets the names of the taxi companies locally)
private void jsonParse() {
String url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.6232817,0.2919483&radius=1000&type=taxi%20service&keyword=taxi&key=APIKEY";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++){
JSONObject result = jsonArray.getJSONObject(i);
String name = result.getString("name");
String id = result.getString("place_id");
placeid.add(id);
listItem.add(name);
}
adapter = new ArrayAdapter(Taxi.this, R.layout.row, listItem);
userlist.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
jsonParseNumber (gets the number and calls it - This is the code which is returning NULL when it is first ran)
private String jsonParseNumber(int pos) {
String id = placeid.get(pos);
String url = "https://maps.googleapis.com/maps/api/place/details/json?placeid="+id+"&key=APIKEY";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonArray = response.getJSONObject("result");
for (int i = 0; i < jsonArray.length(); i++){
number = jsonArray.getString("formatted_phone_number");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
pQueue.add(request);
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel: " + number));
startActivity(intent);
return number;
}
At first I thought it could be that the Dial Action is being called before the Parser finished so I moved it to the end of jsonParseNumber to make sure but it still does the same.
Quick notes:
2 JSON Parsers in Activity
1st Parser gets JSON from GOOGLE PLACES SEARCH API to get names and place id of taxi companies in the area
2nd Parser uses the place id to get a JSON from GOOGLE PLACES DETAILS to get the phone number
When the 2nd Parser is ran it will return a NULL on the first run however if called again it will return the return the intended phone number and dial it.
It is because, your number is null by the time you return "number". You are not waiting until the api call is complete. It is better you implement a callback when you get the response to perform specific action.

I want to create an ArrayList with a dynamic index not starting from 0

public void jsonParse(){
jsonResArray = new ArrayList< >();
String url = "my-api-url";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new
Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
if(jsonArray.length()!=0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id =Integer.parseInt(jsonObject.getString("service_id"));
**jsonResArray.add(id, jsonObject.getString("service_name"));**
}}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getBaseContext() , e.toString() , Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
mQueue.add(request);
}
I want to create dynamic index as i want to save service name against its id in string array but its giving me index out of bp
Thanks in advance
Use a Map<Integer, String> instead of an ArrayList<String>.

Don't update realmResult and not set TextView

I have 4 classes. I got some data from the JSON and wrote the realm. After that, in FilmFeature class, I insert some new values to realm but realmResult don't update and TextView comes to null.
Before this class, We have some information on RealmResult but this information not to "Type" or "imdbID"
FilmFeature Class:
JSONArray array1 = jsonObject2.getJSONArray("Search");
for (int i = 0; i < array1.length(); i++) {
final JSONObject film_ozel = array1.getJSONObject(i);
if(baslik_bilgisi.equals(film_ozel.getString("Title"))) {
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
film film_ozellik = realm.createObject(film.class);
try {
film_ozellik.setType(film_ozel.getString("Type"));
film_ozellik.setImdbID(film_ozel.getString("imdbID"));
realm.insertOrUpdate(film_ozellik);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
realmResult = realm.where(film.class).findAll();
tür.setText(realmResult.get(position).getType());
imdb.setText(realmResult.get(position).getImdbID());

Categories