How can ViewModel react to its own livedata field changes? - java

public class WeatherDataViewModel extends ViewModel {
private MutableLiveData<String> cityName = new MutableLiveData<>();
private MutableLiveData<WeatherData> weatherData = new MutableLiveData<>();
private final WeatherDataService service = new retrofit2.Retrofit.Builder()
.baseUrl("https://api.openweathermap.org")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WeatherDataService.class);
public LiveData<String> getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName.setValue(cityName);
}
// i want to send request everytime cityName ( live data field ) is being changed and inside this function
// i want to update weatherData ( another live data field)
public void updateWeatherData(String cityName, MutableLiveData<WeatherData> weatherData) {
service.getWeatherData(cityName).enqueue(new Callback<WeatherData>() {
#Override
public void onResponse(Call<WeatherData> call, Response<WeatherData> response) {
Log.i("RETROFIT", response.toString());
weatherData.setValue(response.body());
}
#Override
public void onFailure(Call<WeatherData> call, Throwable t) {
Log.e("RETROFIT", t.getMessage());
Log.e("RETROFIT", "Nie udało się pobrać poprawnie danych z API");
}
});
}
}
I want to send request everytime cityName ( live data field ) is being changed and inside this function updateWeatherData method I want to update weatherData ( another live data field)
Can someone help?

Related

how to store json array response using retrofit

I want to call an api endpoint and display the associated response in my android app.
The api takes a parameter user_name and return response of that user.
Response is in json array consisting json of date, location and img (base64).
RESPONSE
[
{
"id": "602a1901a54781517ecb717c",
"date": "2021-02-15T06:47:29.191000",
"location": "mall",
"img": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCASvBLADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/
}
]
the issue I am facing is I am not able to store the response and parse it.
I am using retrofit for this.
There is no issue in the api call the server gets a successfull request the issue is in the client side (android app).
Here is the code that i currently have.
ApiInterface.java
interface ApiInterface {
#FormUrlEncoded
#POST("end_point")
Call<List<Task>>getstatus(#Field("user_name") String user_name);
}
Task.java
public class Task {
#SerializedName("user_name")
public String user_name;
public Task(String user_name) {
user_name = user_name.substring(1, user_name.length()-1);
this.user_name= user_name;
}
public String getUser() {
return user_name;
}
}
MainActivity.java
private void LoginRetrofit(String user_name) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("url")
.addConverterFactory(GsonConverterFactory.create())
.build();
final ApiInterface request = retrofit.create(ApiInterface.class);
Call<List<Task>> call = request.getstatus(user_name);
Toast.makeText(getApplicationContext(), "user_name" + " " + call.request(), Toast.LENGTH_LONG).show();
call.enqueue(new Callback<List<Task>>() {
#Override
public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {
try {
List<Task> rs=response.body();
Toast.makeText(getApplicationContext(), "Response" + " "+rs, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Log.d("REsponse error",e.getMessage());
}
}
#Override
public void onFailure(Call<List<Task>> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
This is the response which is being returned.
In MainActivity.java file, update the code as below as you are printing rs object in your toast message, it prints only the object address not the value in it. So to print the value of the object you received, you must call the methods of the object to get value like as.
MainActivity.java
private void LoginRetrofit(String user_name) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("url")
.addConverterFactory(GsonConverterFactory.create())
.build();
...
call.enqueue(new Callback<List<Task>>() {
#Override
public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {
try {
List<Task> rs=response.body();
if(rs.size() > 0){
Task user = rs.get(0);
String id = user.getId();
String location = user.getLocation();
Toast.makeText(getApplicationContext(),"Response : Id="+id+" and location="+location, Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Log.d("REsponse error",e.getMessage());
}
}
...
});
}
and update the Task model as
Task.java
public class Task {
#SerializedName("id") public String id;
#SerializedName("date") public String date;
#SerializedName("location") public String location;
#SerializedName("img") public String img;
public Task(String id, String date, String location, String img) {
this.id=id;
this.date=date;
this.location=location;
this.img=img;
}
public String getId(){ return this.id; }
public String getDate(){ return this.date; }
public String getLocation(){ return this.location; }
public String getImg(){ return this.img; }
}
Your data model class variables should be serialized or to have names as same as in the response , so that retrofit can be able to bind response values to your model variables .
Also you must have a variable in your model class for each key-value pair in your response that you want to use .
check this example to get how retrofit really work .

Initialize List using getter setter

I am trying to initialize a List using setter method in same class when I will receive a response but when I called getter from other class its returning an empty list. Is it technically possible what I'm trying to achieve?
public class CategoryCommonAPI
{
private Context context;
private String endUrl;
private UserAccessToken userAccessToken;
private String ITV_BASE_URL;
private List<CategoryCommonResponse> itemq;
public CategoryCommonAPI(Context context, String endUrl, List<CategoryCommonResponse> itemq)
{
this.context = context;
this.endUrl = endUrl;
this.itemq = itemq;
ITV_BASE_URL = context.getString(R.string.baseUrl);
userAccessToken = new UserAccessToken(context);
}
public void getAllCategory()
{
Toast.makeText(context, "Iam thanos", Toast.LENGTH_SHORT).show();
String token ="Bearer "+userAccessToken.getAccessToken();
final ITV_API_Service itvApiService = RetrofitClient.getClient(ITV_BASE_URL).create(ITV_API_Service.class);
itvApiService.categoriesItem(endUrl,token).enqueue(new Callback<List<CategoryCommonResponse>>() {
#Override
public void onResponse(Call<List<CategoryCommonResponse>> call, Response<List<CategoryCommonResponse>> response)
{
if (response.isSuccessful())
{
List<CategoryCommonResponse> item = response.body();
Toast.makeText(context, "Iam called", Toast.LENGTH_SHORT).show();
setItem(item);
}
}
#Override
public void onFailure(Call<List<CategoryCommonResponse>> call, Throwable t)
{
Toast.makeText(context, "Image response failed", Toast.LENGTH_SHORT).show();
}
});
}
private void setItem(List<CategoryCommonResponse> item)
{
this.itemq= item;
Log.d("List size", "setItem: "+item.size());
Log.d("Return List", "setItem: "+itemq.size());
}
public List<CategoryCommonResponse> getItemq()
{
return this.itemq;
}
}
and where I'm calling it is
CategoryCommonAPI commonAPI = new CategoryCommonAPI(this, endUrl, list);
commonAPI.getAllCategory();
list=commonAPI.getItemq();
When I checked the list.size() using toast or Log.d it show 0 item.
Since its an asynchronous call and you are trying to retrieve the list immediately after it then one possible way is to return the list after setting from the getAllCategory() method itself.

Retrofit single value / array

this is my web service for sending and receiving a string value with key
Urls.class
public class Urls {
public static final String MAIN_URL="example.com";
}
API.class
public interface API {
#POST("user.php")
Call<MainResponse> registerUser(#Body User user);
#POST("user.php")
Call<MainResponse>loginUser(#Body User user);
#POST("contact.php")
Call<MainResponse>checkNumber(#Body Phone phone);
}
WebService.class
public class WebService {
private static WebService instance;
private API api;
public WebService() {
OkHttpClient client = new OkHttpClient.Builder().build();
Retrofit retrofit = new Retrofit.Builder().client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Urls.MAIN_URL)
.build();
api = retrofit.create(API.class);
}
public static WebService getInstance() {
if (instance == null) {
instance = new WebService();
}
return instance;
}
public API getApi() {
return api;
}
}
MainResponse.class
public class MainResponse {
#SerializedName("status")
public int status;
#SerializedName("message")
public String message;
}
Phone.class
public class Phone {
#SerializedName("phone")
public String phone;
}
MainActivity.class
Phone phone=new Phone();
phone.phone=contactsString[0];
WebService.getInstance().getApi().checkNumber(phone).enqueue(new Callback<MainResponse>() {
#Override
public void onResponse(Call<MainResponse> call, Response<MainResponse> response) {
if (response.body().status==1){
//do something
}
}
#Override
public void onFailure(Call<MainResponse> call, Throwable t) {
}
});
My question is how to edit this to send an array filled with values contactsString[] and receive another array
Your service is in a way to get single request and give you back single response, you should change server side service if you are the backend developer of the service, to get a list of request and give back a list of result
this is your current service:
#POST("contact.php")
Call<MainResponse>checkNumber(#Body Phone phone);
server side developer should change service for you to be able to send in body an object like this for Phones:
public class Phones {
#SerializedName("phones")
public List<String> phones;
}
and in your response you should get list of status and messages with request phones
response like this:
public class MainResponse {
public List<PhoneStatusResponse> phonesStatusList;
}
public class PhoneStatusResponse {
#SerializedName("status")
public int status;
#SerializedName("message")
public String message;
#SerializedName("phoneRequest")
public String phone;
}

How to solve 404 on Retrofit #POST on Django RESTful API?

I am able to get data from database using retrofit and REST api but facing errors in Posting data. Post works using postman but not through retrofit.I have been unable to locate the error.I have tried changing endpoint, that is, "rest" and "rest/" but still get not found error.
ApiView of Post in Django RESTful api: view.py
def post(self,request):
serializer =table_restSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'results':serializer.data},status=201)
return Response(serializer.errors, status=404)
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^rest',views.restSerializer.as_view())
]
urlpatterns = format_suffix_patterns(urlpatterns)
serializer.py:
class table_restSerializer(serializers.ModelSerializer):
class Meta:
model = table_rest
fields = '__all__'
My android code:
Interface
public interface ApiInterface {
#GET("rest")
Call<CustomViewResponse> getJsonFromSid();
#POST("rest")
Call<CustomViewResponse> createTask(#Body CustomViewHolder task);
}
CustomViewHolder class:
public class CustomViewHolder {
#SerializedName("id")
private Integer id;
#SerializedName("tt")
private String tt;
#SerializedName("varr")
private Integer varr;
#SerializedName("edi")
private String edi;
public CustomViewHolder(String tt, Integer varr, String edi){
this.tt = tt;
this.varr = varr;
this.edi = edi;
}
public Integer getid(){
return id;
}
/*public void setid(Integer id){
this.id = id;
}*/
public String gettt()
{
return tt;
}
public void settt(String tt){
this.tt = tt;
}
public Integer getvarr(){
return varr;
}
public void setvarr(Integer varr){
this.varr = varr;
}
public String getedi(){
return edi;
}
public void setedi(String edi){
this.edi = edi;
}
}
CustomViewResponse class
public class CustomViewResponse {
#SerializedName("results")
private List<CustomViewHolder> results;
public List<CustomViewHolder> getResults(){
return results;
}
}
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.sid_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
ApiInterface apiService1 = ApiClient.getClient().create(ApiInterface.class);
Call<CustomViewResponse> call = apiService.getJsonFromSid();
CustomViewHolder cc = new CustomViewHolder("my task title",22,"a string");
call.enqueue(new Callback<CustomViewResponse>() {
#Override
public void onResponse(Call<CustomViewResponse> call, Response<CustomViewResponse> response) {
int statuscode = response.code();
List<CustomViewHolder> customViewHolders = response.body().getResults();
recyclerView.setAdapter(new AdapterSid(customViewHolders, R.layout.list_item_sid, getApplicationContext()));
}
#Override
public void onFailure(Call<CustomViewResponse> call, Throwable t) {
Log.e("TAG main", t.toString());
}
});
call1.enqueue(new Callback<CustomViewResponse>() {
#Override
public void onResponse(Call<CustomViewResponse> call1, Response<CustomViewResponse> respo) {
int statuscode = respo.code();
Log.d("Message", "code..."+respo.code() + " message..." + respo.message());
CustomViewResponse respon = respo.body();
if (respon == null){
Log.e("Error",""+statuscode+ "......"+ respo.message()+"....null body");
}
}
#Override
public void onFailure(Call<CustomViewResponse> call1, Throwable t) {
Log.e(TAG, t.toString());
}
});
}
Following is my table structure:
class table_rest(models.Model):
tt = models.CharField(max_length=10,default = 12)
varr = models.IntegerField(default=30)
edi = models.CharField(max_length=1000,default='44')
def __str__(self):
return self.tt
Using postman my Json body which gets successfully saved is :
{
"tt": "hello",
"varr": 911,
"edi": "emergency. Can't find solution"
}
Please add an extra URL.
urlpatterns = [
url(r'^admin/', admin.site.urls), # Any URL starting with admin(ex: http://testdomainname.com/admin/xyz)
url(r'^rest/$',views.restSerializer.as_view()), # Any URL starting with only rest(ex: http://testdomainname.com/rest/)
url(r'^$',views.restSerializer.as_view()), # Any empty URL with '' (ex: http://testdomainname.com/)
]
First I included the url pattern as suggested by Dinesh Mandepudi. Then I made changes to my regex in retrofit. It was url issue that I was not confronting when using postman. I just added '/' at the beginning of my post regex.
public interface ApiInterface {
#GET("/rest")
Call<CustomViewResponse> getJsonFromSid();
#POST("/rest/")
Call<CustomViewResponse> createTask(#Body CustomViewHolder task);
}
Also a silly mistake is I was trying to add a string of 13 length in the database column while I had set the limit to 10.

Robospice + Retrofit + ORMLite

I'm using Robospice with Retrofit ans ORMLite modules. Retrofit part working good. I have City model for Retrofit:
City.java:
public class City {
public int city_id;
public String name;
#SuppressWarnings("serial")
public static class List extends ArrayList<City> {
}
}
I'm taking this model from server by GET-request:
MyApi.java
public interface MyAPI {
#GET("/cities")
City.List getCities();
}
This part works fine by calling this method:
getSpiceManager().execute(mRequestCity, "city", DurationInMillis.ONE_MINUTE, new ListCityRequestListener());
and listener:
public final class ListCityRequestListener implements RequestListener<City.List> {
#Override
public void onRequestFailure(SpiceException spiceException) {
Toast.makeText(RegisterActivity.this, "failure", Toast.LENGTH_SHORT).show();
}
#Override
public void onRequestSuccess(final City.List result) {
Toast.makeText(RegisterActivity.this, "success", Toast.LENGTH_SHORT).show();
updateCities(result);
}
}
At this time i want to download city list once from server and store this list into sqlitedb by ORMLite module. I've created ORMLite model:
City.java
#DatabaseTable(tableName = "city")
public class City {
public final static String DB_CITY_ID_FIELD_NAME = "id";
public final static String DB_CITY_NAME_FIELD_NAME = "name";
#DatabaseField(canBeNull = false, dataType = DataType.INTEGER, columnName = DB_CITY_ID_FIELD_NAME)
int id;
#DatabaseField(canBeNull = false, dataType = DataType.STRING, columnName = DB_CITY_NAME_FIELD_NAME)
private String name;
public City() {
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("id = ").append(id);
sb.append(", ").append("name = ").append(name);
return sb.toString();
}
}
My RetrofitSpiceService.java looks like this:
public class RetrofitSpiceService extends RetrofitGsonSpiceService {
private final static String BASE_URL = "http://example.com/api/v1";
private final static UserFunctions userFunctions = new UserFunctions();
#Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {
CacheManager cacheManager = new CacheManager();
List< Class< ? >> classCollection = new ArrayList< Class< ? >>();
// add persisted classes to class collection
classCollection.add( City.class );
// init
RoboSpiceDatabaseHelper databaseHelper = new RoboSpiceDatabaseHelper( application, "sample_database.db", 1 );
InDatabaseObjectPersisterFactory inDatabaseObjectPersisterFactory = new InDatabaseObjectPersisterFactory( application, databaseHelper, classCollection );
cacheManager.addPersister( inDatabaseObjectPersisterFactory );
return cacheManager;
}
#Override
protected Builder createRestAdapterBuilder() {
Builder mBuilder = super.createRestAdapterBuilder();
mBuilder.setRequestInterceptor(new RequestInterceptor() {
#Override
public void intercept(RequestFacade request) {
if (userFunctions.isUserLoggedIn()) {
request.addHeader("Authorization", userFunctions.getToken());
}
}
});
return mBuilder;
}
#Override
public void onCreate() {
super.onCreate();
addRetrofitInterface(MyAPI.class);
}
#Override
protected String getServerUrl() {
return BASE_URL;
}
}
I can't understand how can i store and read data from my City database? How do i need to change RetrofitSpiceService? I want download data by Retrofit and store it to database by ORMLite. My CacheManager is correct, i.e. will work properly? Maybe I misunderstand how the module Robospice-ORMLite works?
Thanks a lot!
When you make execute() call with cache key and duration Robospice will store your response into database.
getSpiceManager().execute(mRequestCity, "city", DurationInMillis.ONE_MINUTE, new ListCityRequestListener());
All following requests during one minute will get data from this cache, and then it makes network call. If you want to get data only from cache take a look on getSpiceManager().getFromCache() method. I think it's what you are looking for.

Categories

Resources