Preciso enviar a string username via post depois do button1 ser clicado, meu código não tem nenhum erro no monitor,simplesmente não envia nada ao banco...Este é meu codigo :
public class PostTeste extends AppCompatActivity {
private Button button1;
private EditText username;
String mUsername;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button1 = (Button) findViewById(R.id.button1);
username = (EditText) findViewById(R.id.etpost);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mUsername = username.getText().toString();
// String mPassword = password.getText().toString();
//tryLogin(mUsername);
new TryLogin().execute("Samara");
}
});
}
private class TryLogin extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
final String mUsername = params[0];
if (null == mUsername) {
return "Username não informado";
}
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "email=" + mUsername;
try {
url = new URL("api_url");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXXXXXXXXXXXXXX");
connection.setRequestProperty("X-DreamFactory-Session-Token", "key");
connection.setRequestProperty("Authorization", "auth");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
/**
Não podemos interar com a tela!, vamos mandar para o método onPostExecute
*/
// Toast.makeText(this,"Message from Server:"+ response, 0).show();
isr.close();
reader.close();
return response;
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
/**
* Este método irá rodar na Thread de UI após executar a ação!
*
* @param s
*/
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
}
}
}