set value of a static field in Intent service - java

I have a static ArrayList categories in MainActivity. I want to load data to category in a IntentService which is call Retrofit 2 response.
when i debugged. Retrofit response work well it return a arraylist of category and add to categories but when I run the project the categories is null.
I donn't know why i get this error. Is there any way to fix it?
MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final String TAG="MainActivity";
public static User user = new User();
public static ArrayList<Category> categories = new ArrayList<>();
private Context context;
TextView textCartItemCount;
AppInfomation appInfo;
Retrofit retrofit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
appInfo = new AppInfomation(context);
retrofit = ApiClient.getApiClient();
Intent intent = new Intent(this, GetCategoryService.class);
startService(intent);
...
GetCategoryService.java.
public class GetCategoryService extends IntentService {
public GetCategoryService(){
super("GetCategoryService");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
Retrofit retrofit = ApiClient.getApiClient();
Api api = retrofit.create(Api.class);
Call<ArrayList<Category>> getCategory = api.getCategories();
try {
Response<ArrayList<Category>> response = getCategory.execute();
for(Category c: response.body()){
MainActivity.categories.add(c);
}
} catch (IOException e) {
Log.d("GetCategoryService", "Cannot call API");
}
}
public void parseCategories(ArrayList<Category> cate){
MainActivity.categories = cate;
}
}

Related

java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., #GET, #POST, etc.).Error when trying call apiInterface GET request

I get error when trying to call API interface get request. I am trying to pass a value through GET method. But I get this error in logcat Caused by: java.lang.IllegalArgumentException: HTTP method annotation is required (e.g., #GET, #POST, etc.). for method ApiInterface.name
Here is my java code. Here I call the function namely getname()to pass a string value through GET request. Here I also have one more function getdata() which display output in the recyclerview.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
ListAdapter1 listAdapter;
// List<SupermarketModels> supermarketModelsList = new ArrayList<>();
ApiInterface apiInterface;
String hi;
Button badd;
EmptyAdapterl emptyAdapter = new EmptyAdapterl();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
badd=findViewById(R.id.btadd);
badd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(MainActivity.this,Insert.class);
startActivity(intent);
}
});
String tablen= getIntent().getStringExtra("name");
// supermarketModelsList.add(new SupermarketModels(1,"sugar cane","20kg",
// "50rs","31/12/2021"));
//
// supermarketModelsList.add(new SupermarketModels(2,"sugar cane","20kg",
// "50rs","31/12/2021"));
// supermarketModelsList.add(new SupermarketModels(3 ,"sugar cane","20kg",
// "50rs","31/12/2021"));
initialization();
hi=tablen;
Toast.makeText(MainActivity.this,"oo"+hi,Toast.LENGTH_SHORT).show();
getname(hi);
// recyclerView.setAdapter(emptyAdapter);
// getdata();
}
private void initialization(){
recyclerView = findViewById(R.id.recyclerview1);
Retrofit retrofit = APIClient.getclient();
apiInterface = retrofit.create(ApiInterface.class);
// apiInterface = APIClient.getRetrofitInstance().create(ApiInterface.class);
}
private void setadapter(List<SupermarketModels> supermarketModels){
listAdapter = new ListAdapter1(this, supermarketModels,hi);
// LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
#SuppressLint("WrongConstant")
RecyclerView.LayoutManager lm = new LinearLayoutManager(getApplicationContext(), LinearLayout.VERTICAL, false);
recyclerView.setLayoutManager(lm);
// linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(lm);
recyclerView.setAdapter(listAdapter);
}
private void getname(String n) {
apiInterface.name(n).enqueue(new Callback<DeleteResponse>() {
#Override
public void onResponse(Call<DeleteResponse> call, Response<DeleteResponse> response) {
try {
if (response.body().getStatus().equals("1")){
Toast.makeText(MainActivity.this,"Successfully inserted",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"Error while inserting",Toast.LENGTH_SHORT).show();
}
} catch (Exception e){
}
}
#Override
public void onFailure(Call<DeleteResponse> call, Throwable t) {
}
});
}
private void getdata(){
apiInterface.getList().enqueue(new Callback<GetListResponse>() {
#Override
public void onResponse(Call<GetListResponse> call, Response<GetListResponse> response) {
try {
if (response!= null){
if (response.body().getStatus().equals("1")){
// category_adapter category_adapter = new category_adapter(getContext(),datalist);
setadapter(response.body().getData());
}
else {
Toast.makeText(MainActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e){
Log.e("exp", e.getLocalizedMessage());
}
}
#Override
public void onFailure(Call<GetListResponse> call, Throwable t) {
}
});
}
}
This is my apiInterface class
public interface ApiInterface {
#GET("user-controller.php?operation=list")
Call<GetListResponse> getList();
Call<DeleteResponse> name(#Field("name") String name1);
}
make sure you are using Retrofit2 in your #GET Imports (maybe you are using the wrong #Get)

Unable to call API from another class using retrofit

I am trying to call an API in APICaller.java from MainActivity.java. And then to put the data into recyclerview in MainActivity.java. However, it seems like it fails to call the API and restaurantList is empty. How can I go about to fix this? Thank you in advance.
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
List<Restaurant> restaurantList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
restaurantList = new ArrayList<>();
APICaller apiCaller = new APICaller();
apiCaller.getRestaurants(new APICaller.OnRestaurantListReceivedCallback() {
#Override
public void onRestaurantListReceived(List<Restaurant> list) {
String name = list.get(0).toString();
PutDataIntoRecyclerView(list);
}
});
}
private void PutDataIntoRecyclerView (List<Restaurant> restaurantList) {
Adapter adapter = new Adapter(this, restaurantList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
APICaller.java
public class APICaller {
private List<Restaurant> restaurantList;
interface OnRestaurantListReceivedCallback{
void onRestaurantListReceived(List<Restaurant> list);
}
public void getRestaurants(OnRestaurantListReceivedCallback callback){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://example123.com/wp-json/wp/")
.addConverterFactory(GsonConverterFactory.create())
.build();
WordpressAPICall wordpressAPICall = retrofit.create(WordpressAPICall.class);
Call<List<Restaurant>> call = wordpressAPICall.getData();
call.enqueue(new Callback<List<Restaurant>>() {
#Override
public void onResponse(Call<List<Restaurant>> call, Response<List<Restaurant>> response) {
List<Restaurant> restaurantModels = response.body();
for(int i = 0; i < restaurantModels.size(); i++){
int id = restaurantModels.get(i).getId();
String title = restaurantModels.get(i).getTitle().getRendered();
restaurantList.add(restaurantModels.get(i));
}
callback.onRestaurantListReceived(restaurantList);
}
#Override
public void onFailure(Call<List<Restaurant>> call, Throwable t) {
Log.d("ON FAILURE API CALL", t.toString());
}
});
}

need to wait answer from retrofit request in MainActivity

i have request in retrofit where return List which assign for my dataSet field. i did it in onResponse method. But initializing of recycle view in MainActivity is faster than request and view is showing nothing. What i can to do for waiting onResponse method.
I have this two methods in class NewsRepository.java
private void setNews(){
GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);
Call<ItemsAPI> call = service.getAllItems();
call.enqueue(new Callback<ItemsAPI>() {
#Override
public void onResponse(Call<ItemsAPI> call, Response<ItemsAPI> response) {
Log.d(TAG, "onResponse");
items = response.body();
dataSet = items.getItems();
Log.d(TAG, dataSet.get(0).getTitle());
}
#Override
public void onFailure(Call<ItemsAPI> call, Throwable t) {
Log.d(TAG, "onFailure "+ t.getMessage());
}
});
}
public MutableLiveData<List<News>> getNews(){
setNews();
MutableLiveData<List<News>> data = new MutableLiveData<>();
data.setValue(dataSet);
return data;
}
And have this in MainActivityModelView.java
public void init(){
if(mNews != null){
return;
}
mRepo = NewsRepository.getInstance();
mNews = mRepo.getNews();
}
And this is MyActivity.java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView mRecycleView;
private RecycleViewAdapter mAdapter;
private MainActivityViewModel mMainActivityViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecycleView = findViewById(R.id.recyclev_view);
mMainActivityViewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class);
mMainActivityViewModel.init();
mMainActivityViewModel.getNews().observe((LifecycleOwner) this, new Observer<List<News>>() {
#Override
public void onChanged(#Nullable List<News> news) {
mAdapter.notifyDataSetChanged();
}
});
mMainActivityViewModel.getIsUpdating().observe((LifecycleOwner) this, new Observer<Boolean>() {
#Override
public void onChanged(#Nullable Boolean aBoolean) {
if(aBoolean){
}
else{
mRecycleView.smoothScrollToPosition(mMainActivityViewModel.getNews().getValue().size()-1);
}
}
});
initRecyclerView();
Log.d(TAG, "RecycleView is inited");
}
private void initRecyclerView(){
mAdapter = new RecycleViewAdapter(this, mMainActivityViewModel.getNews().getValue());
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecycleView.setLayoutManager(linearLayoutManager);
mRecycleView.setAdapter(mAdapter);
}
}
The Activity is init is faster then my request.
2019-05-18 09:32:12.299 13859-13859/com.krasnov.rxjavalearning D/MainActivity: RecycleView is inited
2019-05-18 09:32:13.098 13859-13859/com.krasnov.rxjavalearning D/NewsRepository: onResponse
I think it's better to move this code above othre code below findViewById
initRecyclerView();
Log.d(TAG, "RecycleView is inited");
In this function its better to not use this constractor
mAdapter = new RecycleViewAdapter(this, mMainActivityViewModel.getNews().getValue());
Use this instead (create this constractor)
mAdapter = new RecycleViewAdapter(this);
And when your data arrive fire this function in adapter
public void setData( List<News> list) {
this.list= list
notifyDataSetChanged()
}
So your code is like
mMainActivityViewModel.getNews().observe((LifecycleOwner) this, new Observer<List<News>>() {
#Override
public void onChanged(#Nullable List<News> news) {
mAdapter.setData(news)
}
});
in your onResponse you should notify adapter add this:
yourAdapter.notifyDataSetChanged();
On resume call the retrofit service,
after response create a callback
method in ViewModel to notify in Activty class, after notified then
initialise recyclerview.
.

Calling notifyOnDataSetChanged in AsyncTask (not within the class)

*************PROBLEM FIXED, CHECK BELOW FOR A SOLUTION*************
I have been struggling with that nearly half a day. Cannot get it work properly.
I have AsyncTask with private method, so I can pass boolean and String values in CustomLvAdapter
private void changeJobStatus(final boolean isAppliedforAJob, final String jobID){
class ChangeJobStatus extends AsyncTask<Void,Void,String> {
//private Delegates del = null;
ProgressDialog loading;
#Override
protected void onPreExecute() {
super.onPreExecute();
if(isAppliedforAJob) {
loading = ProgressDialog.show(context, "","Canceling application", false);
}
else {
loading = ProgressDialog.show(context, "","Applying for position", false);
}
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//del.asyncCompleteOnCustomJob(true);
loading.dismiss();
}
#Override
protected String doInBackground(Void... v) {
String res;
HashMap<String,String> params = new HashMap<>();
params.put(Config.KEY_USER_ID, studentID);
params.put(Config.KEY_JOB_ID, jobID);
RequestHandler rh = new RequestHandler();
if(isAppliedforAJob)
res = rh.sendPostRequest(Config.URL_CANCEL_APPLICATION, params);
else
res = rh.sendPostRequest(Config.URL_APPLY_FOR_A_JOB, params);
Log.d("Stringas", "CustomListViewBackground " + res);
return res;
}
}
ChangeJobStatus cjs = new ChangeJobStatus();
cjs.execute();
}
and in onPostExcecute() I want to call notifyOnDataSetChanged() to my another activity lvAdapters.
As far as I read I have to implement delegate interface, but I didnt succeed doing that. I fail at initializing delegate in my main class, because changeJobStatus method is private and it is called in customLvAdapter class.
If I make a constructor in ChangeJobStatus class
public ChangeJobStatus(Delegates delegate)
{
this.del = delegate;
}
I have to pass something in the parameters, when excecuting it. If I pass new Delegate, my delegate implementation, which is in my another activity is not triggered.
ChangeJobStatus cjs = new ChangeJobStatus(new Delegates() {
#Override
public void asyncCompleteOnCustomJob(boolean success) {
//whatever
}
});
cjs.execute();
I hope you can help me figure out right implementation for that,
Cheers
***********SOLUTION***********
Sadly, I couldn't implement what fellow user gave to me, but I am very glad that I heard from one of you I can use broadcast receiver. And it worked.
This is what I did
Create a Broadcast Receiver in your main class
private final BroadcastReceiver broadcastJobList = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//what will happen, when event triggers
}
};
Register custom intent and register it to Broadcast receiver in your main class onCreate method or wherever you feel comfortable :)
IntentFilter filter = new IntentFilter();
filter.addAction("jobListChanged");
registerReceiver(broadcastJobList, filter);
All we left to do is send intent which will trigger Broadcast receiver. Following code in my scenario went to onPostExcecute method in custom adapter (context was initialized for Context at the beggining of custom adapter)
Intent intent = new Intent();
intent.setAction("jobListChanged");
context.sendBroadcast(intent);
Hope I will help anyone that has this problem. Cheers!
// your asynctask class
public class ChangeJobStatus extends AsyncTask<String, Void, String> {
private ProgressDialog loading;
private OnResponseListener responseListener;
private boolean isAppliedforAJob;
private Context con;
public ChangeJobStatus(Context con,boolean state) {
super();
// TODO Auto-generated constructor stub
this.con=con;
isAppliedforAJob = state;
}
public void setOnResponseListener(OnResponseListener onLoadMoreListener) {
this.responseListener = onLoadMoreListener;
}
public interface OnResponseListener {
public void onResponse(String responsecode);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
if (isAppliedforAJob) {
loading = ProgressDialog.show(con, "", "Canceling application", false);
} else {
loading = ProgressDialog.show(con, "", "Applying for position", false);
}
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// del.asyncCompleteOnCustomJob(true);
loading.dismiss();
responseListener.onResponse(s);
}
#Override
protected String doInBackground(String... param) {
String res="";
HashMap<String, String> params = new HashMap<>();
params.put(Config.KEY_JOB_ID, param[0]);// job id
params.put(Config.KEY_USER_ID, param[1]);// student id
RequestHandler rh = new RequestHandler();
if (isAppliedforAJob)
res = rh.sendPostRequest(Config.URL_CANCEL_APPLICATION, params);
else
res = rh.sendPostRequest(Config.URL_APPLY_FOR_A_JOB, params);
return res;
}
}
in your activity class
public class MainActivity extends Activity implements OnResponseListener {
String jobId="1",studId="1";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ChangeJobStatus cbs=new ChangeJobStatus(this, true);
cbs.setOnResponseListener(this);
cbs.execute(jobId,studId);
}
#Override
public void onResponse(String responsecode) {
// TODO Auto-generated method stub
//here u can do ur stuff with the string
}
}

How to pass service object to subclass in Android?

I'm developing an app, which gets data from web and writes it back to web. For that I was using AsyncTask, but those requests weren't optimized, so I had to authenticate user, on every request. Now I want to use Service for holding the connection to backend and to do requests without needing to authenticate the user on every requests.
I'm having trouble to send this service object to subactivity without creating an new instance and nulling all the values I need from the service. So how to do it without osing values ?
Here is my service:
public class LasteMVService extends Service {
private ArrayList<WorkSheet> workSheetArrayList;
private SpreadSheet selectedSpeadSheet;
private static String LOG_TAG = "LasteMVService";
private IBinder mBinder = new MyBinder();
#Override
public void onCreate() {
super.onCreate();
new Thread(new Task()).start();
Log.v(LOG_TAG, "in onCreate");
}
#Override
public IBinder onBind(Intent intent) {
Log.v(LOG_TAG, "in onBind");
return mBinder;
}
#Override
public void onRebind(Intent intent) {
Log.v(LOG_TAG, "in onRebind");
super.onRebind(intent);
}
public SpreadSheet getSelectedSpeadSheet() {
return selectedSpeadSheet;
}
public ArrayList<WorkSheet> getWorkSheetArrayList() {
return workSheetArrayList;
}
#Override
public void onDestroy() {
Log.v(LOG_TAG, "in onDestroy");
super.onDestroy();
}
public class MyBinder extends Binder {
public LasteMVService getService() {
return LasteMVService.this;
}
}
class Task implements Runnable {
#Override
public void run() {
synchronized (this){
AndroidAuthenticator androidAuthenticator = new AndroidAuthenticator(getApplicationContext());
SpreadSheetFactory spf = SpreadSheetFactory.getInstance(androidAuthenticator);
ArrayList<SpreadSheet> sheets = spf.getSpreadSheet("Stardiprotokoll", true);
if (sheets != null) {
final SpreadSheet spreadSheet = sheets.get(0);
selectedSpeadSheet = spreadSheet;
ArrayList<WorkSheet> workSheet = spreadSheet.getAllWorkSheets();
workSheetArrayList = workSheet;
}
}
}
}
}
And here is where I bind it with BaseActivity:
public class BaseActivity extends AppCompatActivity {
protected LasteMVService lasteMVService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lasteMVService = ((LasteMVApplication) getApplicationContext()).getLasteMVService();
if(lasteMVService == null){
((LasteMVApplication) getApplicationContext()).initApp();
lasteMVService = ((LasteMVApplication) getApplicationContext()).getLasteMVService();
Util.restartApp(this, true);
}
}
#Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(BaseActivity.this,LasteMVService.class);
startService(intent);
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
protected ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
LasteMVService.MyBinder myBinder = (LasteMVService.MyBinder) service;
lasteMVService = myBinder.getService();
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
};
My question is how to get the same instance of the service in some of the subactivities of BaseActivity?
Okay so you need a service object, Scratch getting the object's reference from base activity rather create a singleton class that is globally accessible throughout your application..
The source code to help you is below
public class Common extends Application {
private Service mService; //Service Object from your class that extends service.
private Context mContext;
public Service getService(){
return mService;
}
public void setService(Service service){
mService = service;
}
#Override
public void onCreate() {
super.onCreate();
//Gets the context associated with the whole application
mContext = getApplicationContext();
}
}
public class Service extends Service{
private Common mApp;
private Context mContext;
//Instantiate the Common Object from the singleton class within on create.
#Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
mApp = (Common) getApplicationContext();
mApp.setService(this);
}
}
//Now to use it in your base Activity
public class BaseActivity extends AppCompatActivity{
private Common mApp;
private Service mService;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mApp = (Common) mContext.getApplicationContext();
//To use the service you would simply use:
mService = mApp.getService()./*Some public method defined in your service*/;
}
}

Categories

Resources