2016-06-17 2 views
-1

앱이 실행되지만 특정 기능이 없습니다. 로그인 또는 계정 등록을 시도 할 때마다 Gradle 콘솔에 프레임이 건너 뛰고 너무 많이 실행되고 있다고 표시됩니다. 내가하려는 것은 사용자 정보를 받아서 데이터베이스로 보내는 것입니다. 이것은 문제가있는 레지스터 활동 코드입니다. JSON을 꺼내서 새로운 활동을 열어두면 작동합니다. 등록에 대한Android Studio 앱 데이터베이스를 통해 실행되지 않음

Response.Listener<String> responseListener = new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        try { 
         JSONObject jsonResponse = new JSONObject(response); 
         boolean success = jsonResponse.getBoolean("success"); 
         if (success) { 
          Intent intent = new Intent(RegisterActivity.this, LoginActivity.class); 
          RegisterActivity.this.startActivity(intent); 
         } else { 
          AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this); 
          builder.setMessage("Register Failed") 
            .setNegativeButton("Retry", null) 
            .create() 
            .show(); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 

      RegisterRequest registerRequest = new RegisterRequest(username, email, password, responseListener); 
      RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this); 
      queue.add(registerRequest); 
     } 
    }); 
} 
} 

PHP 코드 :

$username = $_POST["username"]; 
$email = $_POST["email"]; 
$password = $_POST["password"]; 

$statement = mysqli_prepare($con, "INSERT INTO data (username, email, password) VALUES (?, ?, ?, ?)"); 
mysqli_stmt_bind_param($statement, "sss", $username, $email, $password); 
mysqli_stmt_execute($statement); 

$response = array(); 
$response["success"] = true; 

echo json_encode($response); 
?> 

등록 요청 코드 :

private Map<String, String> params; 

public RegisterRequest(String username, String email, String password, Response.Listener<String> listener){ 
    /* 
    NExt line means we are going to pass some information into the register.php 
    */ 
    super(Method.POST, REGISTER_REQUEST_URL, listener, null); 
    /* 
    This is how we pass in the information from the register to the thing, we are using a hashmap 
    */ 
    params = new HashMap<>(); 
    params.put("username", username); 
    params.put("email", email); 
    params.put("password", password); 

} 
/* 
Volley needs to get the data so we do a get params 
Which gives us this method 
*/ 

@Override 
public Map<String, String> getParams() { 
    return params; 
} 
} 

사람이 내가이 문제를 해결하는 방법을 알고 있나요 ??? 이 비동기 작업을 입력하는 방법을 모르겠다. 누구든지 도움을 요청하십시오. 어쨌든 비동기 작업없이이 문제를 해결할 수 있습니까? 감사합니다!

답변

0

UI를 렌더링하기 위해 Android 프레임 워크에서 사용하는 스레드 인 메인 스레드에서 네트워크 요청을 실행하고있을 가능성이 큽니다. 다른 스레드에서 네트워킹 작업을 수행하는 메커니즘이 필요합니다. AsyncTask은 모두 구현하기가 가장 간단하며 간단한 작업이므로 시나리오에 유용합니다.

AsyncTask을 확장하고 그것을에 요청 매개 변수를 패스 Map : 자세한 내용은

RegisterTask task = new RegisterTask(); 
task.execute(yourHashMapContainingData); 

체크 아웃이 공식 Google 문서 : 당신은이 같은이 작업을 수행 할 수

public class RegisterTask extends AsyncTask<Map, Void, Boolean> { 

    @Override 
    protected Boolean doInBackground(Map... params) { 
     Map props = params[0]; // you can access your request params here 

     /* 
     Do your network request here, using HttpUrlConnection or 
     HttpClient. and return a result (boolean in this example), 
     which is passed to the onPostExecute method 
     */ 
     return false; 
    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) { 
     // This method is run on the main thread, so you can 
     // update your UI after the request is completed. 
    } 
} 

Perform Network Operations on a Separate Thread

+0

감사합니다. 작동하는지 말해 드리겠습니다 !!! –

+0

작동하는 경우 질문에 답변하지 않도록 답변을 수락하십시오 :) –

관련 문제