0
votes

i want to sava data from android app to local host in my pc. i write a edittext to get my text:

<EditText 
     android:id="@+id/text2"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

and write a method to send data to localhost:

public void send(View v3)
 {
    String msg = edittext2.getText().toString();  

    // make sure the fields are not empty
    if (msg.length()>0)
    {
        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();
    }

}

and call method by sendbutton:

sendButton = (Button) findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v3) {
    // TODO Auto-generated method stub
//  send datasend=new send();
    //datasend.execute();
    send(v3);
}

});

but get a massage that: 08-10 01:14:26.171: V/InputMethodManager(20557): START INPUT: android.widget.EditText{41812b10 VFED..CL .F....ID 0,672-225,731 #7f080013 app:id/text2} ic=com.android.internal.widget.EditableInputConnection@418662b0 tba=android.view.inputmethod.EditorInfo@41866268 controlFlags=#100

i write a php code in server side ti get post variable.

1
Did you inititalize the Edittext2 variable with "findViewById"?nouseforname
yes, i initialize in oncreate method in activitymain classfarzaneh abolqasemi
can you add "android:inputType="text"" to the edit text?!nouseforname
yes, but i get same message!farzaneh abolqasemi

1 Answers

0
votes

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