我想使用名为RegisterActivity
的新类(扩展AsyncTask)将数据从mySqlDatabase
传递到BackgroundTask
。调用onPreExecute()
,doInBackground()
和onPostExecute()
后,onPostExecute
将获得null
值到我的属性JSON。
BackGroundTask
public class BackgroundTask extends AsyncTask<String,Void,String> {
String registerUrl = "http://127.0.0.1/gestion/register.php";
Context ctx ;
Activity activity ;
ProgressDialog progressDialog ;
AlertDialog.Builder builder ;
public BackgroundTask(Context ctx) {
this.ctx = ctx;
activity = (Activity)ctx;
}
@Override
protected void onPreExecute() {
builder = new AlertDialog.Builder(activity);
progressDialog = new ProgressDialog(ctx);
progressDialog.setTitle("Please Wait ");
progressDialog.setMessage("Connecting Server ... ");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String method = params[0];
if (method.equals("register")){
try {
URL url = new URL(registerUrl);
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 name = params[1];
String email = params[2];
String password = params[3];
String data = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "" ;
while ((line=bufferedReader.readLine())!=null){
stringBuilder.append(line+"\n");
}
httpURLConnection.disconnect();
Thread.sleep(5000);
return stringBuilder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String json) {
if(json == null){
try {
progressDialog.dismiss();
showDialog("Registration Failed ", "Json Null" , "reg_false" );
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray =jsonObject.getJSONArray("server_response");
JSONObject jo = jsonArray.getJSONObject(0);
String code = jo.getString("code");
String message = jo.getString("message");
if(code.equals("reg_true")){
showDialog("Registration sucess ", message , code );
}else if(code.equals("reg_false")){
showDialog("Registration Failed ",message,code);
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
showDialog("Registration Failed ", "Json not Null" , "reg_false" );
}
}
public void showDialog(String title , String message, String code ){
builder.setTitle(title);
if(code.equals("reg_true")|| code.equals("reg_false")){
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
activity.finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
RegisterActivity
public void initGUI(){
name = (EditText) findViewById(R.id.reg_name);
email = (EditText) findViewById(R.id.reg_email);
password = (EditText) findViewById(R.id.reg_password);
conPassword = (EditText) findViewById(R.id.reg_con_password);
register =(Button)findViewById(R.id.btn_reg);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initGUI();
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (name.getText().toString().equals("") || email.getText().toString().equals("") || password.getText().toString().equals("")|| conPassword.getText().toString().equals("")){
builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setTitle("Mooooochkila 1 ");
builder.setMessage(" walm3alam 3amar kolchi ola may3bak hal");
builder.setPositiveButton("Wakha", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
else if (!(password.getText().toString().equals(conPassword.getText().toString()))){
builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setTitle("Mooooochkila 2 ");
builder.setMessage("3awati lpassword Ghalet 3awd 4awdo");
builder.setPositiveButton("Wakha", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
password.setText("");
conPassword.setText("");
}
}
);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
else{
BackgroundTask backgroundTask = new BackgroundTask(RegisterActivity.this);
backgroundTask.execute("register", name.getText().toString(), email.getText().toString(),password.getText().toString());
}
}
});
}
答案 0 :(得分:0)
如果您使用默认的Android模拟器进行测试,请将10.0.2.2放入您的网址;如果您使用的是genemotion模拟器,请将10.0.2.2放入网址。 127.0.0.1是您的环回或本地主机地址。但是你需要把你的模拟器或PC的地址。