0

我有一项服务正在运行以获取 API 值并将其存储在 sql lite DB 中。我有一个单独的排球类和一个界面。

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);
}

}

我的服务等级:

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);
}

}

界面:

public interface VolleyJsonRespondsListener {

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

如果我的输入如下所示,我可以在实现接口时在 Service 中接收 json 响应。

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

我需要响应的类:

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) {

}
}

现在,我想在 Service.this 以外的另一个类中接收响应。我尝试在第二个参数中使用类名,但我收到空指针异常。如何通过实现接口接收其他类的响应?

4

0 回答 0