2017-03-21 93 views
0

我目前正在爲我的研究創建一個Android應用程序。這個應用程序必須發送和接收來自數據庫的數據。使用不同類的Android發送JSon對象

爲此,我有一個MySQL數據庫和一個Apache服務器。我的Android應用程序使用JSon與服務器通信,服務器執行PHP腳本與數據庫通信。

我試圖做一個簡單的「登錄」模塊,但我不知道如何在Android中的兩個類中進行通信。隨着我的代碼,它會更清晰我認爲:

public class Inscription extends AppCompatActivity { 
    Utilisateur u = new Utilisateur(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.inscription); 

     Button connexion = (Button) findViewById(R.id.buttonInscription); 
     connexion.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       EditText inputLogin = (EditText) findViewById(R.id.editTextLogin); 
       EditText inputMdp = (EditText) findViewById(R.id.editText3Mdp); 
       EditText inputEmail = (EditText) findViewById(R.id.editTextMail); 

       Log.v("Login input: ", inputLogin.getText().toString()); 

       Utilisateur u = new Utilisateur(); 
       u.setLogin(inputLogin.getText().toString()); 
       u.setMdp(inputMdp.getText().toString()); 
       u.setMail(inputEmail.getText().toString()); 

       Log.v("User: ", u.toString()); 

       new InscriptionServeur().execute(); 
       Intent intent = new Intent(Inscription.this, Inscription.class); 
       startActivity(intent); 

      } 
     }); 

    } 


    class InscriptionServeur extends AsyncTask<String, String, String> { 
     private final String url = "http://xxx.xxx.xxx.xxx/xxxx/scriptInscription.php"; 
     private final OkHttpClient client = new OkHttpClient(); 
     private Gson gson = new Gson(); 
     private String c; 

     protected String doInBackground(String... args) { 
      try { 
       post(url); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return ""; 
     } 

     public String post(String url) throws IOException { 
      // Prepare the request. 
      c=gson.toJson(u); 
      // MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8"); 
      MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8"); 
      Log.v("Json: ", c); 
      Log.v("User bis: ", u.toString()); 

      Request myGetRequest = new Request.Builder() 
        .url(url) 
        .post(RequestBody.create(JSON_TYPE, c)) 
        .build(); 
      // Get the result. 
      client.newCall(myGetRequest).enqueue(new Callback() { 
       @Override 
       public void onFailure(Call call, IOException e) { 
        Context context = getApplicationContext(); 

        Toast.makeText(context, "FAIL", Toast.LENGTH_LONG); 
       } 

       @Override 
       public void onResponse(Call call, Response response) throws IOException { 
        Context context = getApplicationContext(); 

        Toast.makeText(context, "PASS", Toast.LENGTH_LONG); 
       } 
      }); 
      return null; 

     } 
    } 
} 

所以,你可以看到,它很簡單。 我的問題是,當我登錄第一個「用戶」(在onCreate()方法)時,用戶完全顯示,但是當我在post()方法中使用同一個用戶時(在InscriptionServeur類中) ,它什麼也沒有顯示,所以PHP腳本不會工作,因爲它不接受空表單。

你能幫我嗎?我真的被困在這裏

感謝

回答

0

您已經聲明ü在兩個地方,一個全球性的代碼的開始和一個又一個的setOnClickListener內。

從你的聽衆刪除此行Utilisateur u = new Utilisateur();,它應該工作

+1

@Liberti太感謝你了,它的工作原理! – toximorph