获取所有JSON响应,然后启动另一个活动

时间:2014-09-25 04:12:26

标签: android json android-activity android-asynctask

我正在进行套接字编程并获得JSON响应。一切都工作得很好,但唯一让我遇到麻烦的是我在得到回应之前开始了另一项活动,但我希望得到所有回复,然后开始另一项活动。 这是我的代码。

jsonobject1.put("username", edt.getText().toString());
                jsonobject1.put("udid",
                        "A892E0AB-6732-4F42-BEFA-3157315E9EE4");
                try {

                    socket.emit("setPseudo", jsonobject1);
                    socket.emit("findAllUsers", jsonobject1);
                    Log.e("TAG",""+ socket.getId());

                    Intent intent = new Intent(MainActivity.this,
                            MenuScreen.class);
                    intent.putExtra("onlineuser", onlineuser);
                    intent.putExtra("finduser", finduserjson);
                    startActivity(intent);

在上面的代码中,我将JSON数据发送到服务器并获取JSON对象作为响应。但在收到回复之前,我被送往另一项活动。所以我首先要反应,然后开始活动。帮我一些伪代码。

由于

2 个答案:

答案 0 :(得分:1)

创建AsyncTask类

public class GetJSONResult extends AsyncTask<String, Void, Void>
    {
        ProgressDialog pd ;

        private Context _context;

        public GetJSONResult(Context c)
        {
            _context = c;
        }

        protected void onPreExecute()
        {
            super.onPreExecute();
            pd = new ProgressDialog(_context);
            pd.setTitle("Getting JSON details");
            pd.setMessage("Please wait...");
            pd.setCancelable(false);
            pd.setIndeterminate(true);
            pd.show();
        }

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub

            try
            {
                 jsonobject1.put("username", params[0]);    // params[0] is the value passed i.e edittext value 
            jsonobject1.put("udid", 
                    "A892E0AB-6732-4F42-BEFA-3157315E9EE4")
               socket.emit("setPseudo", jsonobject1); 
                socket.emit("findAllUsers", jsonobject1); 
                Log.e("TAG",""+ socket.getId());
            }

            catch (Exception e)
            {
                if (pd.isShowing())
                    pd.dismiss();

            }

            return null;

        }


        protected void onPostExecute(Void v)
        {
            super.onPostExecute(v);
            try
            {
                if (pd.isShowing())
                    pd.dismiss();
            }
            catch(Exception e)
            {

            }

             Intent intent = new Intent(MainActivity.this,
                        MenuScreen.class); 
                intent.putExtra("onlineuser", onlineuser);
                intent.putExtra("finduser", finduserjson);
                startActivity(intent);
        }
    }

将MainActivity表单调用AsyncTask,如下所示

public MainActivity extends Activity
{

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.your_layout);
        // First get the reference to EditText using findViewById, then
       String s = edt.getText().toString();

      // Call the AsyncTask

      new GetJSONResult(MainActivity.this).execute(s);   // pass the edittext value to doInBackGround method.

    }
}

答案 1 :(得分:0)

private class getResponse extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {

      jsonobject1.put("username", edt.getText().toString());
            jsonobject1.put("udid",
                    "A892E0AB-6732-4F42-BEFA-3157315E9EE4");
            try {

                socket.emit("setPseudo", jsonobject1);
                socket.emit("findAllUsers", jsonobject1);
                Log.e("TAG",""+ socket.getId());

            } catch (Exception e) {

                e.printStackTrace();
            }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }

         Intent intent = new Intent(MainActivity.this,
                        MenuScreen.class);
                intent.putExtra("onlineuser", onlineuser);
                intent.putExtra("finduser", finduserjson);
                startActivity(intent);

    }

    @Override
    protected void onCancelled() {

        super.onCancelled();
        progressDialog.dismiss();

    }

}

并执行新的getResponse()。execute();