2016-11-26 2 views
0

로그인 및 등록 후 show alert messages가 어떻게 다른지 알려드립니다. onPreExecute()로 인해 로그인 성공 후 alertDialog 로그인과 등록도 표시됩니다. 예외를 사용하려고했지만 실패했습니다. 어떻게해야합니까? onPreExecute() 만드는 방법은 무엇입니까?

은 BackgroundTask.java

public class BackgroundTask extends AsyncTask<String,Void,String> { 
    AlertDialog alertDialog; 
    Context ctx; 

    BackgroundTask(Context ctx) { 
     this.ctx = ctx; 
    } 

    protected void onPreExecute() { 
      alertDialog = new AlertDialog.Builder(ctx).create(); 
      alertDialog.setTitle("Loading"); 
      alertDialog.setMessage("Load Success"); 
      alertDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     String reg_url = "http://35.160.135.119/webapp/register.php"; 
     String login_url = "http://35.160.135.119/webapp/login.php"; 
     String method = params[0]; 
     if (method.equals("register")) { 
      String user = params[1]; 
      String user_name = params[2]; 
      String user_pass = params[3]; 
      try { 
       URL url = new URL(reg_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       //httpURLConnection.setDoInput(true); 
       OutputStream OS = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
       String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") + "&" + 
         URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
         URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       OS.close(); 
       InputStream IS = httpURLConnection.getInputStream(); 
       IS.close(); 
       //httpURLConnection.connect(); 
       httpURLConnection.disconnect(); 
       return "Registration Success..."; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } else if (method.equals("login")) { 
      String login_name = params[1]; 
      String login_pass = params[2]; 
      try { 
       URL url = new URL(login_url); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
       String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" + 
         URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
       String response = ""; 
       String line = ""; 
       while ((line = bufferedReader.readLine()) != null) { 
        response += line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return response; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (result.equals("Registration Success...")) { 
      Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
     } else { 
      alertDialog.setMessage(result); 
      alertDialog.show(); 
     } 
    } 
} 

이 LoginActivity입니다.

public class LoginActivity extends Activity { 
    EditText ET_NAME,ET_PASS; 
    String login_name,login_pass; 

    @Override 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     ET_NAME = (EditText)findViewById(R.id.user_name); 
     ET_PASS = (EditText)findViewById(R.id.user_pass); 
    } 
    public void userReg(View view) 
    { 
     startActivity(new Intent(this,Register.class)); 
    } 
    public void userLogin(View view) 
    { 
     login_name = ET_NAME.getText().toString(); 
     login_pass = ET_PASS.getText().toString(); 
     String method = "login"; 
     BackgroundTask backgroundTask = new BackgroundTask(this); 
     backgroundTask.execute(method,login_name,login_pass); 

     Intent intent = new Intent(this, MainActivity.class); 
     intent.putExtra("ID", login_name); 
     intent.putExtra("PW", login_pass); 
     startActivity(intent); 
    } 
} 

그리고 이것은 등록 클래스입니다.

public class Register extends Activity { 
    EditText ET_NAME, ET_USER_NAME, ET_USER_PASS; 
    String user, user_name, user_pass; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.register_layout); 
     ET_NAME = (EditText) findViewById(R.id.user); 
     ET_USER_NAME = (EditText) findViewById(R.id.new_user_name); 
     ET_USER_PASS = (EditText) findViewById(R.id.new_user_pass); 
    } 

    public void userReg(View view) { 
     user = ET_NAME.getText().toString(); 
     user_name = ET_USER_NAME.getText().toString(); 
     user_pass = ET_USER_PASS.getText().toString(); 
     String method = "register"; 
     BackgroundTask backgroundTask = new BackgroundTask(this); 
     backgroundTask.execute(method, user, user_name, user_pass); 
     finish(); 
    } 
} 

답변

0

죄송합니다. 장기 실행 작업을 실행하기 전에 수행 할 작업. 예를 들어 ProgessDialog를 보여줍니다.

protected void onPreExecute() { 
    ProgressDialog progress = ProgressDialog.show(this, "dialog title", 
    "dialog message", true); 
    progress.show(); 
} 

@Override 
protected String doInBackground(String... params) { 
    String reg_url = "http://35.160.135.119/webapp/register.php"; 
    String login_url = "http://35.160.135.119/webapp/login.php"; 
    String method = params[0]; 
    if (method.equals("register")) { 
    String user = params[1]; 
    String user_name = params[2]; 
    String user_pass = params[3]; 
    try { 
     URL url = new URL(reg_url); 
     HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
     httpURLConnection.setRequestMethod("POST"); 
     httpURLConnection.setDoOutput(true); 
     //httpURLConnection.setDoInput(true); 
     OutputStream OS = httpURLConnection.getOutputStream(); 
     BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
     String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8") + "&" + 
     URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" + 
     URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8"); 
     bufferedWriter.write(data); 
     bufferedWriter.flush(); 
     bufferedWriter.close(); 
     OS.close(); 
     InputStream IS = httpURLConnection.getInputStream(); 
     IS.close(); 
     //httpURLConnection.connect(); 
     httpURLConnection.disconnect(); 
     return "Registration Success..."; 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } else if (method.equals("login")) { 
    String login_name = params[1]; 
    String login_pass = params[2]; 
    try { 
     URL url = new URL(login_url); 
     HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
     httpURLConnection.setRequestMethod("POST"); 
     httpURLConnection.setDoOutput(true); 
     httpURLConnection.setDoInput(true); 
     OutputStream outputStream = httpURLConnection.getOutputStream(); 
     BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
     String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" + 
     URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8"); 
     bufferedWriter.write(data); 
     bufferedWriter.flush(); 
     bufferedWriter.close(); 
     outputStream.close(); 
     InputStream inputStream = httpURLConnection.getInputStream(); 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
     String response = ""; 
     String line = ""; 
     while ((line = bufferedReader.readLine()) != null) { 
     response += line; 
     } 
     bufferedReader.close(); 
     inputStream.close(); 
     httpURLConnection.disconnect(); 
     return response; 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
    return null; 
} 

@Override 
protected void onProgressUpdate(Void... values) { 
    super.onProgressUpdate(values); 
} 

@Override 
protected void onPostExecute(String result) { 
    if (result.equals("Registration Success...")) { 
    Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); 
    } else { 
    alertDialog = new AlertDialog.Builder(ctx).create(); 
    alertDialog.setTitle("Loading"); 
    alertDialog.setMessage("Load Success"); 
    alertDialog.show(); 
    alertDialog.setMessage(result); 
    alertDialog.show(); 
    } 
} 
0

당신은 당신이 호출되는 서비스 지정 BackgroundTaskconstructor를 사용할 수 있습니다. 로그인 요청에 대한

public class BackgroundTask extends AsyncTask<String,Void,String> { 
    AlertDialog alertDialog; 
    Context ctx; 
    String request; 
    public static final int TYPE_LOGIN = 1; 
    public static final int TYPE_REGISTER = 2; 
    private int type; 

    BackgroundTask(Context ctx, int type) { 
     this.ctx = ctx; 
     this.type = type; 
    } 

    protected void onPreExecute() { 
     alertDialog = new AlertDialog.Builder(ctx).create(); 
     if(type == TYPE_LOGIN){ 
      alertDialog.setTitle("Loading"); 
      alertDialog.setMessage("Logging you in"); 
     } else if (type == TYPE_REGISTER) { 
      alertDialog.setTitle("Loading"); 
      alertDialog.setMessage("Registering your account"); 
     } 
     alertDialog.show(); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     alertDialog.setTitle("Success"); 
     if(type == TYPE_LOGIN){ 
      alertDialog.setMessage("Login Success"); 
     } else if (type == TYPE_REGISTER) { 
      alertDialog.setMessage("Registration Success"); 
     } 
     alertDialog.show(); 
     ... 
    } 
} 

사용, 등록 요청에 대한

BackgroundTask backgroundTask = new BackgroundTask(this, BackgroundTask.TYPE_LOGIN); 

사용,

BackgroundTask backgroundTask = new BackgroundTask(this, BackgroundTask.TYPE_REGISTER); 
관련 문제