2016-04-26 43 views
0

我有這段代碼對我來說很好,並且可以訪問數據庫。唯一的問題是,我想添加消息,敬酒,或者在重複的主鍵值時顯示錯誤...根據Json響應添加Toast

這是我的asyncTask的代碼(我添加了Toast,但沒有工作:()

Button con=(Button)findViewById(R.id.inscription); 
con.setOnClickListener(new OnClickListener() { 
    public void onClick(View v){ 
     new CreateNewUser().execute(); 
     } 
    }); 
} 

class CreateNewUser extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

protected String doInBackground(String... args) { 
    @SuppressWarnings("deprecation") 
    Date d=new Date(an-2800,mn,jn); 
    Date d1=new Date(ap-2800,mp,jp); 
    String datenaiss=d.toString(); 
    String deliv=d1.toString(); 
    EditText pseud=(EditText) findViewById(R.id.pseud); 
    String pseudo = pseud.getText().toString(); 
    EditText name=(EditText) findViewById(R.id.nom); 
    String nom = name.getText().toString(); 
    EditText prenom=(EditText) findViewById(R.id.pren); 
    String pren =prenom.getText().toString(); 
    EditText cinn=(EditText) findViewById(R.id.cin); 
    String cin = cinn.getText().toString(); 
    EditText ag=(EditText) findViewById(R.id.age); 
    String age = ag.getText().toString(); 
    EditText tele=(EditText) findViewById(R.id.tel); 
    String tel = tele.getText().toString(); 
    EditText mail=(EditText) findViewById(R.id.email); 
    String email = mail.getText().toString(); 
    EditText adress=(EditText) findViewById(R.id.adresse); 
    String adresse = adress.getText().toString(); 
    EditText motdp=(EditText) findViewById(R.id.pwd); 
    String pwd = motdp.getText().toString(); 
    EditText vill=(EditText) findViewById(R.id.ville); 
    String ville = vill.getText().toString(); 
    EditText numpermi=(EditText) findViewById(R.id.numperm); 
    String numperm = numpermi.getText().toString();   
    String x="http://192.168.1.5/add_user.php"; 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("pseudo", pseudo)); 
    params.add(new BasicNameValuePair("mdp", pwd)); 
    params.add(new BasicNameValuePair("datenaiss", datenaiss)); 
    params.add(new BasicNameValuePair("deliv", deliv)); 
    params.add(new BasicNameValuePair("nom", nom)); 
    params.add(new BasicNameValuePair("prenom", pren)); 
    params.add(new BasicNameValuePair("cin", cin)); 
    params.add(new BasicNameValuePair("age", age)); 
    params.add(new BasicNameValuePair("tel", tel)); 
    params.add(new BasicNameValuePair("email", email)); 
    params.add(new BasicNameValuePair("adresse", adresse)); 
    params.add(new BasicNameValuePair("ville", ville)); 
    params.add(new BasicNameValuePair("numperm", numperm)); 
    JSONObject json; 

    try { 
     json = jsonParser.makeHttpRequest(x,"POST", params); 
     Log.d("Create Response", json.toString()); 
     try { 
      int success = json.getInt(TAG_SUCCESS); 
      if (success == 1) { 
       Toast.makeText(MainActivity.this,"Ajouté avec succés", Toast.LENGTH_LONG).show();} 
      else 
       Toast.makeText(getBaseContext(),"echec",Toast.LENGTH_LONG).show();} 
     catch(JSONException e) { 
      e.printStackTrace(); 
     } 

    } catch (JSONException e1) { 
    // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

// check log cat fro response 
return null;} 
protected void onPostExecute(String file_url) { 
    // dismiss the dialog once done 
} 

這裏是我的PHP ADD_USER文件的代碼: ...........

// mysql inserting a new row 
$result = mysql_query("INSERT INTO utilisateur VALUES('$pseudo', '$mdp', '$nom', '$prenom','$cin', '$datenaiss', '$tel', '$email', '$adresse', '$ville', '$numperm', '$deliv')"); 

// check if row inserted or not 
if ($result) { 
    // successfully inserted into database 
    $response["success"] = 1; 
    $response["message"] = "Product successfully created."; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // failed to insert row 
    $response["success"] = 0; 
    $response["message"] = "Oops! An error occurred."; 

    // echoing JSON response 
    echo json_encode($response); 
} 
}?> 

你們能告訴我,我能做些什麼?如果有不同的方式來做同樣的事情,我很樂意嘗試它們:D

+0

的JSON可以爲空,所以如果你試圖解析它,它會去你的漁獲物和那裏你可以把烤麪包 –

+0

我感謝您的效應初探但是,當用戶添加一個已經存在的主鍵(僞)的值時,我可以添加什麼來獲得一個arror? – AbdallahJg

+0

所以它不是你的android代碼的一部分,它現在在PHP的部分 –

回答

0

您無法觸摸後臺線程中的ui。要顯示Toast,您必須使用ui線程方法運行。或者使用異步任務的進度更新方法。 併爲空值保持檢查。如果這種情況也會發生異常,並可能會崩潰的應用程序。

0

您試圖在doInBackground方法中創建一個Toast,但顯然不能這樣做,因爲doInBackground是從後臺線程調用的。敬酒只能在UI線程中創建。所以,我會建議的是處理您的服務器響應和在UI線程內執行的onPostExecute方法內創建Toasts。你可以做到以下幾點:

class MyCystomObject { 

    private JSONObject json; 
    private String file_url; 

public MyCystomObject(JSONObject json, String file_url) { 
    this.json = json; 
    this.file_url = file_url; 
} 

public void setJson(JSONObject json) { this.json = json; } 

public void setFile_url(String file_url) { this.file_url = file_url; } 

public JSONObject getJson() { return json; } 

public String getFile_url() { return file_url; } 
} 

創建這個類來保存你的服務器和「FILE_URL」字符串(我不知道它是如何準確地傳遞給onPostExecute),並把它傳遞給onPostExecute收到JSON對象。所以,doInBackground和onPostExecute可以看起來像:

protected String doInBackground(String... args) { 

    .... 
    JSONObject json; 
    MyCystomObject myCystomObject; 

    json = jsonParser.makeHttpRequest(x,"POST", params); 
    Log.d("Create Response", json.toString()); 

    myCystomObject = new MyCustomObject(json, your_file_url); 

    return myCystomObject; 
    } 



    protected void onPostExecute(MyCystomObject myCystomObject) { 
     // here you can handle the responce you receive from server and display the appropriate toast message... 
     JSONObject json = myCustomObject.getJson(); 
     try { 
     int success = json.getInt(TAG_SUCCESS); 
     if (success == 1) { 
      Toast.makeText(MainActivity.this,"Ajouté avec succés", Toast.LENGTH_LONG).show();} 
     else 
      Toast.makeText(getBaseContext(),"echec",Toast.LENGTH_LONG).show();} 
    catch(JSONException e) { 
     e.printStackTrace(); 
    } 
    }