I have a problem with this code I must do to make a connection to a php file with a post. Do I have to insert a thread? If yes, why? And where?
public class Login extends Activity {
public String RispostaLogin;
public String response,responseBody;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button pulsante = (Button) findViewById(R.id.button2);
pulsante.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("myapp", "cliccato");
sendPostRequest();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendPostRequest()
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://api.uniparthenope.it/user/radius/auth");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "user"));
nameValuePairs.add(new BasicNameValuePair("passw", "pass"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseBody=EntityUtils.toString(response.getEntity());
Log.d("myapp", responseBody);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}