creating retrofit instance failed
My app uses an android service in which
I create a retrofit instance and make a webservice call, whenever i get a firebase notification.
My app works fine in foreground and background
receives notification and the webservice is also triggered.
But when my app is in kill condition.
I check if the android service is running, if not I start the service and send a broadcast receiver to trigger the webservice call, but i receive the following error.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.Object retrofit2.Retrofit.create(java.lang.Class)'
on a null object reference
Is this because, retrofit instance cannot be created in app kill condition.
how can i fix this?
Thanks in advance
public class FCM_Background extends Service {
private static String urlString;
private static String orderBaseUrl, orderUrl, orderMethod, receiveBaseUrl, receiveUrl, receivedMethod;
private static Context context;
private BroadcastReceiver receiver;
private String orderId;
private ArrayList<Integer> orderid;
DecimalFormat form = new DecimalFormat("#.##");
private Toast toastMessage;
private String paymentType;
private String orderComment;
private String LogTag = FCM_Background.class.getSimpleName();
private SQLiteHelper helper;
public static SQLiteDatabase db;
private String location_id;
private String state;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
orderid = new ArrayList<Integer>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
location_id = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.LOCATION_ID);
state = intent.getExtras().getString("FROM");
fetchJsonDataFromRetrofit();
}
};
registerReceiver(receiver, new IntentFilter(Common.FCM_BROADCAST));
return super.onStartCommand(intent, flags, startId);
}
public synchronized void fetchJsonDataFromRetrofit() {
helper = new SQLiteHelper(getApplicationContext());
db = helper.getWritableDatabase();
final ApiInterface listApi = new ApiClient().getClient().create(ApiInterface.class);
Common.validationToken = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.AUTH_TOKEN);
if (null == orderBaseUrl || null == orderUrl) {
orderBaseUrl = MyPreferenceManager.getInstance(this).getData(Common.DOMAIN_NAME);
orderUrl = "admin/api/orders";
}
final Call OrderCall = listApi.getOrderDetails(orderBaseUrl, orderUrl, Common.validationToken);
OrderCall.enqueue(new Callback<OrderView>() {
@Override
public void onResponse(Call<OrderView> call, Response<OrderView> response) {
orderBaseUrl = null;
orderUrl = null;
if (response.isSuccessful()) {
ArrayList<Order> orders = response.body().getOrderList();
int orderSize = orders != null ? orders.size() : 0;
String orderType = null;
String orderCreatedDate = null;
if (orderSize > 0) {
orderDetails(orders, orderType, orderCreatedDate);
}
} else {
ActionOnTokenExpiry(response);
}
}
@Override
public void onFailure(Call<OrderView> call, Throwable t) {
}
});
}
android retrofit instance
add a comment |
My app uses an android service in which
I create a retrofit instance and make a webservice call, whenever i get a firebase notification.
My app works fine in foreground and background
receives notification and the webservice is also triggered.
But when my app is in kill condition.
I check if the android service is running, if not I start the service and send a broadcast receiver to trigger the webservice call, but i receive the following error.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.Object retrofit2.Retrofit.create(java.lang.Class)'
on a null object reference
Is this because, retrofit instance cannot be created in app kill condition.
how can i fix this?
Thanks in advance
public class FCM_Background extends Service {
private static String urlString;
private static String orderBaseUrl, orderUrl, orderMethod, receiveBaseUrl, receiveUrl, receivedMethod;
private static Context context;
private BroadcastReceiver receiver;
private String orderId;
private ArrayList<Integer> orderid;
DecimalFormat form = new DecimalFormat("#.##");
private Toast toastMessage;
private String paymentType;
private String orderComment;
private String LogTag = FCM_Background.class.getSimpleName();
private SQLiteHelper helper;
public static SQLiteDatabase db;
private String location_id;
private String state;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
orderid = new ArrayList<Integer>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
location_id = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.LOCATION_ID);
state = intent.getExtras().getString("FROM");
fetchJsonDataFromRetrofit();
}
};
registerReceiver(receiver, new IntentFilter(Common.FCM_BROADCAST));
return super.onStartCommand(intent, flags, startId);
}
public synchronized void fetchJsonDataFromRetrofit() {
helper = new SQLiteHelper(getApplicationContext());
db = helper.getWritableDatabase();
final ApiInterface listApi = new ApiClient().getClient().create(ApiInterface.class);
Common.validationToken = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.AUTH_TOKEN);
if (null == orderBaseUrl || null == orderUrl) {
orderBaseUrl = MyPreferenceManager.getInstance(this).getData(Common.DOMAIN_NAME);
orderUrl = "admin/api/orders";
}
final Call OrderCall = listApi.getOrderDetails(orderBaseUrl, orderUrl, Common.validationToken);
OrderCall.enqueue(new Callback<OrderView>() {
@Override
public void onResponse(Call<OrderView> call, Response<OrderView> response) {
orderBaseUrl = null;
orderUrl = null;
if (response.isSuccessful()) {
ArrayList<Order> orders = response.body().getOrderList();
int orderSize = orders != null ? orders.size() : 0;
String orderType = null;
String orderCreatedDate = null;
if (orderSize > 0) {
orderDetails(orders, orderType, orderCreatedDate);
}
} else {
ActionOnTokenExpiry(response);
}
}
@Override
public void onFailure(Call<OrderView> call, Throwable t) {
}
});
}
android retrofit instance
if app is in background there is restriction on starting a service in the new android os version. If you need to do something in the background when process is killed you should look at workmanager.
– Raghunandan
Nov 15 '18 at 8:18
Can you show some code?
– Harry
Nov 15 '18 at 10:13
add a comment |
My app uses an android service in which
I create a retrofit instance and make a webservice call, whenever i get a firebase notification.
My app works fine in foreground and background
receives notification and the webservice is also triggered.
But when my app is in kill condition.
I check if the android service is running, if not I start the service and send a broadcast receiver to trigger the webservice call, but i receive the following error.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.Object retrofit2.Retrofit.create(java.lang.Class)'
on a null object reference
Is this because, retrofit instance cannot be created in app kill condition.
how can i fix this?
Thanks in advance
public class FCM_Background extends Service {
private static String urlString;
private static String orderBaseUrl, orderUrl, orderMethod, receiveBaseUrl, receiveUrl, receivedMethod;
private static Context context;
private BroadcastReceiver receiver;
private String orderId;
private ArrayList<Integer> orderid;
DecimalFormat form = new DecimalFormat("#.##");
private Toast toastMessage;
private String paymentType;
private String orderComment;
private String LogTag = FCM_Background.class.getSimpleName();
private SQLiteHelper helper;
public static SQLiteDatabase db;
private String location_id;
private String state;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
orderid = new ArrayList<Integer>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
location_id = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.LOCATION_ID);
state = intent.getExtras().getString("FROM");
fetchJsonDataFromRetrofit();
}
};
registerReceiver(receiver, new IntentFilter(Common.FCM_BROADCAST));
return super.onStartCommand(intent, flags, startId);
}
public synchronized void fetchJsonDataFromRetrofit() {
helper = new SQLiteHelper(getApplicationContext());
db = helper.getWritableDatabase();
final ApiInterface listApi = new ApiClient().getClient().create(ApiInterface.class);
Common.validationToken = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.AUTH_TOKEN);
if (null == orderBaseUrl || null == orderUrl) {
orderBaseUrl = MyPreferenceManager.getInstance(this).getData(Common.DOMAIN_NAME);
orderUrl = "admin/api/orders";
}
final Call OrderCall = listApi.getOrderDetails(orderBaseUrl, orderUrl, Common.validationToken);
OrderCall.enqueue(new Callback<OrderView>() {
@Override
public void onResponse(Call<OrderView> call, Response<OrderView> response) {
orderBaseUrl = null;
orderUrl = null;
if (response.isSuccessful()) {
ArrayList<Order> orders = response.body().getOrderList();
int orderSize = orders != null ? orders.size() : 0;
String orderType = null;
String orderCreatedDate = null;
if (orderSize > 0) {
orderDetails(orders, orderType, orderCreatedDate);
}
} else {
ActionOnTokenExpiry(response);
}
}
@Override
public void onFailure(Call<OrderView> call, Throwable t) {
}
});
}
android retrofit instance
My app uses an android service in which
I create a retrofit instance and make a webservice call, whenever i get a firebase notification.
My app works fine in foreground and background
receives notification and the webservice is also triggered.
But when my app is in kill condition.
I check if the android service is running, if not I start the service and send a broadcast receiver to trigger the webservice call, but i receive the following error.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.Object retrofit2.Retrofit.create(java.lang.Class)'
on a null object reference
Is this because, retrofit instance cannot be created in app kill condition.
how can i fix this?
Thanks in advance
public class FCM_Background extends Service {
private static String urlString;
private static String orderBaseUrl, orderUrl, orderMethod, receiveBaseUrl, receiveUrl, receivedMethod;
private static Context context;
private BroadcastReceiver receiver;
private String orderId;
private ArrayList<Integer> orderid;
DecimalFormat form = new DecimalFormat("#.##");
private Toast toastMessage;
private String paymentType;
private String orderComment;
private String LogTag = FCM_Background.class.getSimpleName();
private SQLiteHelper helper;
public static SQLiteDatabase db;
private String location_id;
private String state;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
orderid = new ArrayList<Integer>();
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
location_id = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.LOCATION_ID);
state = intent.getExtras().getString("FROM");
fetchJsonDataFromRetrofit();
}
};
registerReceiver(receiver, new IntentFilter(Common.FCM_BROADCAST));
return super.onStartCommand(intent, flags, startId);
}
public synchronized void fetchJsonDataFromRetrofit() {
helper = new SQLiteHelper(getApplicationContext());
db = helper.getWritableDatabase();
final ApiInterface listApi = new ApiClient().getClient().create(ApiInterface.class);
Common.validationToken = MyPreferenceManager.getInstance(getApplicationContext()).getData(Common.AUTH_TOKEN);
if (null == orderBaseUrl || null == orderUrl) {
orderBaseUrl = MyPreferenceManager.getInstance(this).getData(Common.DOMAIN_NAME);
orderUrl = "admin/api/orders";
}
final Call OrderCall = listApi.getOrderDetails(orderBaseUrl, orderUrl, Common.validationToken);
OrderCall.enqueue(new Callback<OrderView>() {
@Override
public void onResponse(Call<OrderView> call, Response<OrderView> response) {
orderBaseUrl = null;
orderUrl = null;
if (response.isSuccessful()) {
ArrayList<Order> orders = response.body().getOrderList();
int orderSize = orders != null ? orders.size() : 0;
String orderType = null;
String orderCreatedDate = null;
if (orderSize > 0) {
orderDetails(orders, orderType, orderCreatedDate);
}
} else {
ActionOnTokenExpiry(response);
}
}
@Override
public void onFailure(Call<OrderView> call, Throwable t) {
}
});
}
android retrofit instance
android retrofit instance
edited Nov 20 '18 at 11:24
jennifer sashi
asked Nov 15 '18 at 8:16
jennifer sashijennifer sashi
53
53
if app is in background there is restriction on starting a service in the new android os version. If you need to do something in the background when process is killed you should look at workmanager.
– Raghunandan
Nov 15 '18 at 8:18
Can you show some code?
– Harry
Nov 15 '18 at 10:13
add a comment |
if app is in background there is restriction on starting a service in the new android os version. If you need to do something in the background when process is killed you should look at workmanager.
– Raghunandan
Nov 15 '18 at 8:18
Can you show some code?
– Harry
Nov 15 '18 at 10:13
if app is in background there is restriction on starting a service in the new android os version. If you need to do something in the background when process is killed you should look at workmanager.
– Raghunandan
Nov 15 '18 at 8:18
if app is in background there is restriction on starting a service in the new android os version. If you need to do something in the background when process is killed you should look at workmanager.
– Raghunandan
Nov 15 '18 at 8:18
Can you show some code?
– Harry
Nov 15 '18 at 10:13
Can you show some code?
– Harry
Nov 15 '18 at 10:13
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315008%2fcreating-retrofit-instance-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315008%2fcreating-retrofit-instance-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
if app is in background there is restriction on starting a service in the new android os version. If you need to do something in the background when process is killed you should look at workmanager.
– Raghunandan
Nov 15 '18 at 8:18
Can you show some code?
– Harry
Nov 15 '18 at 10:13