0

I have a service which is running to get the API values and store it in the sql lite DB. I have a separate class for post volley and a interface.

class PostVolleyJsonRequest {
private String type;
Service act;
private Service service;
Cryptomain cryptomain;
private VolleyJsonRespondsListener volleyJsonRespondsListener;
private String networkurl;
private JSONObject jsonObject = null;
private JSONObject params;


PostVolleyJsonRequest(Service act, VolleyJsonRespondsListener volleyJsonRespondsListener, String type, String netnetworkUrl, JSONObject params) {
    this.act = act;
    this.volleyJsonRespondsListener = volleyJsonRespondsListener;
    this.type = type;
    this.networkurl = netnetworkUrl;
    this.params = params;
sendRequest();
}

private void sendRequest() {

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, networkurl, params,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    volleyJsonRespondsListener.onSuccessJson(response, type);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    try {
                        NetworkResponse response = error.networkResponse;
                        Log.e("response", "response " + response);
                        if (response != null) {
                            int code = response.statusCode;

                            String errorMsg = new String(response.data);
                            Log.e("response", "response" + errorMsg);
                            try {
                                jsonObject = new JSONObject(errorMsg);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            String msg = jsonObject.optString("message");
                            volleyJsonRespondsListener.onFailureJson(code, msg);
                        } else {
                            String errorMsg = error.getMessage();
                            volleyJsonRespondsListener.onFailureJson(0, errorMsg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

    AppController.getInstance().addToRequestQueue(jsObjRequest);
}

}

My service class:

public class Service extends android.app.Service {

private Timer timer = new Timer();


public Service() throws ClassNotFoundException {
}

@Override
public void onCreate() {
    super.onCreate();

    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            sendRequestToServer();   //Your code here
        }
    }, 0, 60*1000);//5 Minutes
}

private void sendRequestToServer() {
    new PostVolleyJsonRequest(Service.this, Service.this,"Zay","https://www.xxxxxx.com/api", null);

}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, flags, startId);
}

}

Interface:

public interface VolleyJsonRespondsListener {

public void onSuccessJson(JSONObject result, String type);
public void onFailureJson(int responseCode, String responseMessage);
}

I am able to receive the json response inside Service while implementing the interface if my input is like below.

new PostVolleyJsonRequest(Service.this, Service.this,"Zay","https://www.xxxxxx.com/api", null);

Class where i need the reponse:

public class main extends AppCompatActivity implements VolleyJsonRespondsListener{

private List<dispitems> movieList = new ArrayList<dispitems>();
private RecyclerView recyclerView;
private Adapter mAdapter;
List<Note> notes;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);


    startService(new Intent(this, Service.class));

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(manager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    mAdapter = new Adapter(movieList);

    recyclerView.setAdapter(mAdapter);


     prepareMovieData();


}

private void prepareMovieData() {
   notes = Select.from(Note.class).fetch();


    if(!notes.isEmpty()) {

        movieList.add(new dispitems(notes.get(0).getBuy(), notes.get(0).getSell()));


    }

    mAdapter.notifyDataSetChanged();
}


@Override
public void onSuccessJson(JSONObject result, String type) {

        //I need the response here
}

@Override
public void onFailureJson(int responseCode, String responseMessage) {

}
}

Now, i want to receive the response in another class other than Service.this. I tried with class name in the 2nd parameter but i am receiving null pointer exception. How can i receive the response in other class by implementing interface?

pfx
  • 20,323
  • 43
  • 37
  • 57
user2269164
  • 1,095
  • 2
  • 15
  • 31
  • where you want a response ?add that class – MJM Apr 11 '18 at 10:29
  • I added the class in question. – user2269164 Apr 11 '18 at 10:33
  • i think you have to use `BroadcastReceiver` https://stackoverflow.com/questions/12997463/send-intent-from-service-to-activity – MJM Apr 11 '18 at 10:36
  • You are initialization the request in the service and implementing the Response listener in the Activity. That is wrong, well either you call the api in the activity then you are able to receive the data in the activity. And the other solution is broadcast listener to implement in service and activity. But why you need an service to call the api ?? – Malik Umar Apr 11 '18 at 10:43
  • I need to update adapter with new list values . – user2269164 Apr 11 '18 at 10:45

0 Answers0