1
我想使用一個名爲BackgroundTask
(它擴展了AsyncTask)的新類,將數據從RegisterActivity
傳遞到mySqlDatabase
。在調用onPreExecute()
,doInBackground()
和onPostExecute()
後,onPostExecute
正在爲我的屬性JSON獲取null
值。onPostExecute變空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
我認爲你的服務器url可能有問題。 – Cool7