Callback failed in android

I am creating an SDK.

Scenario

  • In the application, the developer passes the JsonObject and url in the method and inside the SDK.
  • I add these values ​​to a SQLite database and run JobScheduler.
  • Jobscheduler command accepts a query with 0 indexes from the database and executes it.
  • When I get a response, I delete this query from the database, and now the query with index 1 comes to index 0, and again I execute the same code where the 0 th index query is triggered.

Problem

  • When I receive a response from the server inside the SDK, I need to send it to the developer using a callback.
  • I can accept the callback as an argument when I take the JSON and the url from the user, but I don't know how to proceed further because I cannot save it to the database
  • Suppose I have 5 queries in the database and the scheduler executes them one by one, I don't know how to send a response to the developer. I am unable to pass context to jobcheduler. The only way to do this is to get the appropriate context for each row (query) in the database.

What i tried

  • I tried to use LocalBroadcastManager , but I cannot create a generic class and implement its onreceive () method, and also the problem of passing the context.
  • Tried using Realm as a database to add a Context type and use my model, but it doesn't work as Context is not supported.
  • Storing the operation name in the database with other data, and then retrieving the class from it and the action from the class. And then activating the activity in my callback. but it throws a ClassCastException because the class it is trying to extract from the name is in the developer app and not in my SDK

..

MainActivity Application Code

sdkClass.queueRequest(jo.toString(),"url1",12,"MainActivity");

sdkClass.queueRequest(jo2.toString(),"url2",13,"MainActivity");

sdkClass.queueRequest(jo.toString(),"url3",14,"MainActivity");

sdkClass.executeQueue();

      

SDK class that adds and runs code

public void queueRequest(String payload, String URL, int requestId, String identifier){
    RequestsDatabase database = new RequestsDatabase(context);
    database.insertItem(TxnType,payload,url, String.valueOf(requestId), identifier);
}


public JobScheduler getQueueInstance(Context context){

    if(jobScheduler==null){
        jobScheduler = (JobScheduler)context.getSystemService(JOB_SCHEDULER_SERVICE);
    }
    return jobScheduler;
}
public void executeQueue(){
    getQueueInstance(context);
    ComponentName jobService = new ComponentName(context.getPackageName(), MyJobService.class.getName());
    JobInfo jobInfo = new JobInfo.Builder(1,jobService).setPersisted(true).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY). build();
    jobScheduler.schedule(jobInfo);
}

      

MyJobServiceCode

@Override
public boolean onStartJob(JobParameters params) {

    // UtilityMethods.showToast(this,params.getExtras().getString("json"));
    Toast.makeText(this,"test", Toast.LENGTH_SHORT).show();
    database = RequestsDatabase.getInstance(this);
    ALlRequests = database.getAllItemId();
    executeCall();

    return false;
}
public void executeCall(){
    ALlRequests = database.getAllItemId();
    if(ALlRequests.size()>0) {
        requestModel = ALlRequests.get(0);

        try {
            String payload = getPayload(this, requestModel.getTxnType(), requestModel.getPayload());
            SilverLineRequest req = new SilverLineRequest(this, requestModel.getUrl(), payload, Integer.parseInt(requestModel.getRequestId()), this);
            req.executeToServer();
            Toast.makeText(getApplicationContext(), "requested", Toast.LENGTH_LONG).show();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    else{
        Toast.makeText(this, "Queue empty", Toast.LENGTH_LONG).show();
    }

}
@Override
public void onSuccess(String response, int requestId) {
    if(requestId ==Integer.parseInt(requestModel.getRequestId())){
        Toast.makeText(this,"responded", Toast.LENGTH_SHORT).show();
        database.deleteItem(requestModel.getTxnType());

        // HERE I WANT TO SEND THE USER RESPONSE LIKE
        // listener.onTransactionSuccess(response)
        // BUT I DON'T HAVE 'listener' SO IT SHOWS NULLPOINTER


        SDKClass sl = new SDKClass(this);
        sl.executeQueue();
    }
}

      

Any help will be blessed right now.

+3


source to share


1 answer


You are storing queries in the database, so it can have a unique identifier (autoincrement). Then you can store the callbacks in memory with the request id -> callback. In response, you can call him.

If you'd like, return the ID to the developers. Then they can bind and untie and get results when they need it, even the answer was received yesterday.



Have a look at TransferUtility from Amazon AWS SDK for example.

0


source







All Articles