I hope you do the Http Request in a thread outside of the main gui thread.
Can you try this code:
MainActivity:
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
EditText edittext2;
Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext2 = (EditText) findViewById(R.id.text2);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
send();
}
});
}
public void send() {
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (!msg.equals(""))
{
Log.d(TAG, "send: " + msg);
// HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost("https://localhost/datalog.php");
//
// try {
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// nameValuePairs.add(new BasicNameValuePair("id", "12345"));
// nameValuePairs.add(new BasicNameValuePair("message", msg));
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httpclient.execute(httppost);
// edittext2.setText(""); // clear text box
// } catch (ClientProtocolException e) {
// // TODO Auto-generated catch block
// } catch (IOException e) {
// // TODO Auto-generated catch block
// // Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
// }
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<EditText
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/sendButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="send"
android:layout_below="@id/text2"/>
</RelativeLayout>
This is working well for me, i am interested if its same for you
Edit after get more information:
I tested it now with the simple app and another php file at my computer.
The app is connectiong to the php file, which creates a message file. This message file can be accessed eg via Browser.
The Http part have to run in an task which i did this way:
MainActivity:
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
EditText edittext2;
Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext2 = (EditText) findViewById(R.id.text2);
sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyHttpTask mTask = new MyHttpTask();
mTask.execute("");
}
});
}
class MyHttpTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
send();
return "";
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, "onPostExecute" + result);
edittext2.setText(result);
}
}
public void send() {
String msg = edittext2.getText().toString();
// make sure the fields are not empty
if (!msg.equals(""))
{
Log.d(TAG, "send: " + msg);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.178.60/datalog.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
//edittext2.setText(""); // clear text box
} catch (ClientProtocolException e) {
Log.d(TAG, e.toString());
} catch (IOException e) {
Log.d(TAG, e.toString());
}
}
else
{
// display message if text fields are empty
Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
}
}
}
Make sure its NOT localhost do to this is nor possible to connect except theAndroid device is running the http server with php.
php file datalog.php:
<?php
$message=$_POST['message'];
$filename="androidmessages.html";
file_put_contents($filename,$message."<br />",FILE_APPEND);
$androidmessages=file_get_contents($filename);
echo $androidmessages;
?>
I also get a file called "androidmessages.html" with the text i key in into edittext
For me this is working well without any error
The sense of this job would be to have a web page which is logging the messages from the android device. So it would be possible to view the messages from any browser via network connection