I'm currently creating an Android app for my studies. This app have to send and receive data to and from a database.
To do so, I have a MySQL database and an Apache server. My Android app uses JSon to communicate with the server, and the server execute PHP script to communicate with database.
I'm trying to make a simple "sign in" module, but I don't know how to communicate within two classes in Android. With my code it will be more clear I think:
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;
}
}
}
So as you can see, it's quite simple. My problem is, when I log the first "User" (in the onCreate() method), the user is perfectly show, but when i do the same log with the same User in the post() method (in InscriptionServeur class), it shows nothing, and so the PHP script wont works as it doesn't accept empty form.
Could you please help me ? I'm really stuck here
thanks