android-async-http从onSuccess方法返回json值

时间:2015-03-17 18:32:36

标签: java android json api android-async-http

我一直在研究这个应用程序,我使用android-async-http lib,当我尝试从API发送给我的json中返回值时出现问题。

所以,我有一个名为SyncNotesHandler的类,它执行所有API请求,需要返回一个内部带有json的对象。 这是班级:

public class SyncNotesHandler {

    public SyncNotesHandler(Context context){
        this.context = context;
    }

    public void getNotesFromuser(String token){
        Log.i("Debuggin", "comes around here");

        Header[] headers = {
                new BasicHeader("Authorization",token)
        };


        client.get(this.context, "http://104.131.189.224/api/notes", headers, null, new JsonHttpResponseHandler() {


            public void onSuccess(int statusCode, Header[] headers, JSONObject json) {
                Log.i("Debuggin","getting response");
                try {
                    Log.i("Debuggin", "Response: "+json.toString());
                    note.setTitle(json.toString());
                }  catch (Exception e) {
                    Log.i("Debuggin","error");
                }

            }


            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.i("Debuggin","FAILURE");
            }
        });
        Log.i("Debuggin","pre-final :"+note.getTitle());
    }

    public Note getNoteResponse(){
        Log.i("Debuggin","final :"+note.getTitle());
        return this.note;
    }

    public void start(){
        this.note = new Note();
        this.notes = new ArrayList<Note>();
        this.client = new AsyncHttpClient();
        this.status = 0;
    }

    public void end(){
        this.note = null;
        this.notes = null;
        this.client = null;
        this.status = 0;
    }


    private static AsyncHttpClient client;
    private Note note;
    private ArrayList<Note> notes;
    private Context context;
    private int status;
}

这里我称之为所有行动:

SyncNotesHandler s = new SyncNotesHandler(getApplicationContext());
s.start();
s.getNotesFromuser("e6bc9f7c1e74cb3dd8ccde07e6edbc32");
Note note = s.getNoteResponse();
s.end();

如果您看到代码,我认为代码应遵循的流程是: 输入方法getNotesFromUser() - &gt;在onSuccess方法中执行操作 - &gt;输入方法getNoteResponse(),日志历史记录应为:

Debuggin: comes around here
Debuggin: getting response
Debuggin: Response: {json}
Debuggin: pre-final: {json}
Debuggin: final: {json}

但是真实的日志历史显示了其他东西,它很奇怪,我不知道如何解决这个问题:

    03-17 12:17:56.441    7369-7369/com.example.usuario.androidadmin I/Debuggin﹕ comes around here
03-17 12:17:56.451    7369-7369/com.example.usuario.androidadmin I/Debuggin﹕ pre-final :null
03-17 12:17:56.451    7369-7369/com.example.usuario.androidadmin I/Debuggin﹕ final :null
03-17 12:17:56.878    7369-7369/com.example.usuario.androidadmin I/Debuggin﹕ getting response
03-17 12:17:56.885    7369-7369/com.example.usuario.androidadmin I/Debuggin﹕ Response: {"notes":[{"tags":[],"id":1,"body":"JJJJJJJJJJJJJJJJJJJJJJJJJJJJJ","title":"QUE PEPSI","favorite":true,"status":false}]}

所以问题是onSucces来自另一个进程,首先需要完成从syncNotesHandler调用的操作,然后执行onSuccess。 所以问题是,我需要返回json值,但我不知道该怎么做。

0 个答案:

没有答案