2016-06-11 4 views
2

私は自分のアプリケーションで登録アクティビティを持っています。これには、ユーザーID、電子メール、パスワード、モバイル番号の入力があります。私はUIを作成しました。アプリケーションにasyncTaskコードを追加するには?

コード:

public class RegisterActivity extends AppCompatActivity { 

    TextView already; 
    Button signUp; 
    RelativeLayout parent; 

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

     parent = (RelativeLayout)findViewById(R.id.parentPanel); 

     setupUI(parent); 

     already = (TextView)findViewById(R.id.alreadyRegistered); 
     signUp = (Button) findViewById(R.id.sign_up_button); 

     already.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       startActivity(new Intent(RegisterActivity.this,LoginActivity.class)); 

      } 
     }); 


     signUp.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       startActivity(new Intent(RegisterActivity.this,LoginActivity.class)); 

      } 
     }); 


    } 
    public static void hideSoftKeyboard(Activity activity) { 
     InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); 
    } 
    public void setupUI(View view) { 

     //Set up touch listener for non-text box views to hide keyboard. 
     if(!(view instanceof EditText)) { 

      view.setOnTouchListener(new View.OnTouchListener() { 

       public boolean onTouch(View v, MotionEvent event) { 
        hideSoftKeyboard(RegisterActivity.this); 
        return false; 
       } 

      }); 
     } 

     //If a layout container, iterate over children and seed recursion. 
     if (view instanceof ViewGroup) { 

      for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 

       View innerView = ((ViewGroup) view).getChildAt(i); 

       setupUI(innerView); 
      } 
     } 
    } 
} 

今、私は、サーバーで、このUIを同期したいです。

このため、別のアクティビティで作成されたasyncTaskのコードがあります。このコードをどのように呼び出すか、このコードをUIで実装できますか?

AsyncTaskコード:RegisterActivity

public class RegisterActivity extends AppCompatActivity { 
    Context context; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 
     context = this; 

     RegisterAsyncTask task = new RegisterAsyncTask(); 
     String userPhoto = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlLBAIHAGdIMrN7hH1jKkmZz+d7MPu15md6PtCyrHmqvsgNVjY7Djh69OgwEaU1pkVwanKK0NLSsgvA8Vk="; 

     HashMap<String, String> params = new HashMap<String, String>(); 
     params.put("userUsername", "user1"); 
     params.put("userPassword", "user1"); 
     params.put("gender", "M"); 
     params.put("birthDate", "1986/7/12"); 
     params.put("religion", "Hindu"); 
     params.put("nationality", "Indian"); 
     params.put("motherTongue", "Marathi"); 
     params.put("birthPlace", "Pune"); 
     params.put("userCountry", "India"); 
     params.put("userState", "Maharashtra"); 
     params.put("userCity", "Nashik"); 
     params.put("userPincode", "422101"); 
     params.put("userEmailid", "[email protected]"); 
     params.put("userMobileNo", "9696323252"); 
     params.put("userPhoto", userPhoto); 
    } 
    public class RegisterAsyncTask extends AsyncTask<Map<String, String>, Void, JSONObject>{ 
     @Override 
     protected JSONObject doInBackground(Map<String, String>... params) { 
      try { 
       String api = context.getResources().getString(R.string.server_url) + "api/user/register.php"; 
       Map2JSON mjs = new Map2JSON(); 
       JSONObject jsonParams = mjs.getJSON(params[0]); 
       ServerRequest request = new ServerRequest(api, jsonParams); 
       return request.sendRequest(); 
      } catch(JSONException je) { 
       return Excpetion2JSON.getJSON(je); 
      } 
     } 

     @Override 
     protected void onPostExecute(JSONObject jsonObject) { 
      super.onPostExecute(jsonObject); 

      Log.d("ServerResponse", jsonObject.toString()); 
      try { 
       int result = jsonObject.getInt("result"); 
       String message = jsonObject.getString("message"); 
       if (result == 1) { 
        Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
        //Code for having successful result for register api goes here 
       } else { 
        Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
        //Code when api fails goes here 
       } 

      } catch(JSONException je) { 
       je.printStackTrace(); 
       Toast.makeText(context, je.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 
} 

どのように私はこれを同期することができますか?助けてください。ありがとうございました。

EDIT:

getEventsAsyncTask:

public class GetEventsAsyncTask extends AsyncTask<Void, Void, JSONObject> { 
    String api; 
    private Context context; 

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


    @Override 
    protected JSONObject doInBackground(Void... params) { 
     try { 
      api = context.getResources().getString(R.string.server_url) + "api/event/getEvents.php"; 
      ServerRequest request = new ServerRequest(api); 
      return request.sendGetRequest(); 
     } catch(Exception e) { 
      return Excpetion2JSON.getJSON(e); 
     } 
     } //end of doInBackground 

     @Override 
     protected void onPostExecute(JSONObject response) { 
      super.onPostExecute(response); 
      Log.e("ServerResponse", response.toString()); 
      try { 
       int result = response.getInt("result"); 
       String message = response.getString("message"); 
       if (result == 1) { 
        Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
        //code after getting profile details goes here 
       } else { 
        Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 
        //code after failed getting profile details goes here 
       } 
      } catch(JSONException je) { 
       je.printStackTrace(); 
       Toast.makeText(context, je.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } //end of onPostExecute 
} 

ダイアログ:

@Override 
    protected Dialog onCreateDialog(int id) { 

     Dialog dialog = null; 
     String[] listContent = {"Wedding", 
     "Anniversary", 
     "Naming Ceremony/Baptism", 
     "Thread Ceremony", 
     "Engagement", 
     "Birthday", 
     "Friends and Family Meet", 
     "Funeral", 
     "Movie", 
     "Play"}; 

     switch(id) { 
      case CUSTOM_DIALOG_ID: 
       dialog = new Dialog(PlanEventActivity.this); 
       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
       dialog.setContentView(R.layout.choose_event_dialog); 
       dialog.setCancelable(true); 
       dialog.setCanceledOnTouchOutside(true); 
       dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){ 

        @Override 
        public void onCancel(DialogInterface dialog) { 
// TODO Auto-generated method stub 

        }}); 

       dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){ 

        @Override 
        public void onDismiss(DialogInterface dialog) { 
// TODO Auto-generated method stub 

        }}); 

//Prepare ListView in dialog 
       dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist); 
       ArrayAdapter<String> adapter 
         = new ArrayAdapter<String>(this, 
         android.R.layout.simple_list_item_1, listContent); 
       dialog_ListView.setAdapter(adapter); 
       dialog_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 

         chooseEventText.setText(parent.getItemAtPosition(position).toString()); 

         dismissDialog(CUSTOM_DIALOG_ID); 
        }}); 

       break; 
     } 

     return dialog; 
    } 

このダイアログでasyncTaskからイベントを表示したいです。ありがとうございました。

答えて

3

あなたの質問を正しく理解していますが、AsyncTaskを実行するには、RegisterAsyncTaskのインスタンスを作成して、​​メソッドを呼び出す必要があります。

それとも、あなたは、インスタンスのネタを取得する必要がない場合:

new RegisterAsyncTask().execute(yourMap); 
0

あなたは単にあなたのログイン・アクティビティー・コードでAsyncTaskを伴って含む、あなたのハッシュマップのオブジェクトを入れて、単純にAsyncTaskを呼び出すことができます以下のようにします。

HashMap<String, String> params = new HashMap<String, String>(); 
     params.put("userUsername", "user1"); 
     params.put("userPassword", "user1"); 
     params.put("gender", "M"); 
     params.put("birthDate", "1986/7/12"); 
     params.put("religion", "Hindu"); 
     params.put("nationality", "Indian"); 
     params.put("motherTongue", "Marathi"); 
     params.put("birthPlace", "Pune"); 
     params.put("userCountry", "India"); 
     params.put("userState", "Maharashtra"); 
     params.put("userCity", "Nashik"); 
     params.put("userPincode", "422101"); 
     params.put("userEmailid", "[email protected]"); 
     params.put("userMobileNo", "9696323252"); 
     params.put("userPhoto", userPhoto); 


//call asynctask like this. 
RegisterAsyncTask task = new RegisterAsyncTask(); 
     task.execute(params); 
関連する問題