You should make your HTTP request in a separate thread. NetworkOnMainThreadException
says that you are trying to make your network operation on the main thread.
1] Use simple Thread
Thread t = new Thread(new Runnable() {
@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost("http://bbpf.bplaced.net/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}
});
t.start();
2] You can use AsyncTask if you would like to have more control over the task:
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
ttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost("http://bbpf.bplaced.net/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// Notifies UI when the task is done
textView.setText("Insert finished!");
}
}.execute();
3] Use some networking library (robospice, retrofit) to make writing async network tasks a little easier.