2016-06-17 15 views
-1

アプリは動作しますが、特定の機能はありません。 ログインやアカウント登録をしようとすると、Gradleコンソールにフレームがスキップされ、実行が多すぎると表示されます。 私がしようとしているのは、ユーザー情報を取り込んでデータベースに送信することです。これは、問題があるレジスタアクティビティコードです。 JSONを取り出して、新しいアクティビティを開いているだけであれば動作します。登録のためのAndroid Studio Appの実行に失敗しました

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 Frameworkによって使用されるスレッド)でネットワーク要求を実行している可能性があります。別のスレッドでネットワーキングタスクを行う何らかのメカニズムが必要です。 AsyncTaskは単純なタスクなので、実装するのが最も簡単でシナリオに適しています。

AsyncTaskを拡張し、Mapにそれにあなたのリクエストパラメータを渡す:

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. 
    } 
} 

あなたはこのように、このタスクを実行することができます。

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

詳細については、この公式のGoogleドキュメントをチェックアウト: Perform Network Operations on a Separate Thread

+0

ありがとうございました! –

+0

問題が解決したら、答えを受け入れて質問が閉じられるようにしてください:) –

関連する問題