0

Using API 23. Can't provide LogCat ( i don't have an emulator, i can only test by generating apk). I've been trying these for all day and I am still stuck. Android App > send some Strings to a php located in my localhost ( using xampp)> get back those strings and display in TextView.

MainActivity

public class MainActivity extends AppCompatActivity {

Button b1;
TextView t1;
EditText e1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    e1 = (EditText)findViewById(R.id.editText);
    t1 = (TextView)findViewById(R.id.textView);
     b1 = (Button) findViewById(R.id.button);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
            GetText();
            } catch (Exception ex) {
                t1.setText("url exception");
            }
        }
    });
}

public void GetText() throws UnsupportedEncodingException {

    String nombre = e1.getText().toString();
    new SignIn().execute(nombre);
}

public class SignIn extends AsyncTask {

    @Override
    protected String doInBackground(String... params) {
        String link="http://localhost:8012/kb_test/ejemplo.php";

        String code = params[0];
        StringBuilder sb = new StringBuilder();
        try {
            String data  = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(code, "UTF-8");

            URL url = new URL(link);
            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            wr.write( data );
            wr.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader (conn.getInputStream()));


            String line = null;

            while((line = reader.readLine()) != null)
            {
                sb.append(line);
                break;
            }
            return sb.toString();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }
        return sb.toString();
    }
    @Override
    protected void onPostExecute(String result){
        Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG);
    }
}

.xml

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/editText" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="141dp" />

.php

<?php 

   $name   = urldecode($_POST['name']);
  /* $user   = urldecode($_POST['user']);
   $email  = urldecode($_POST['email']);
   $pass   = urldecode($_POST['pass']);*/


  echo 'Name '. $name /*'Email: '.$email' User'. $user ' Pass  :'. $pass*/; 

?>

** I am aware that i have to enter my actuall IP in Localhost. ** Internet permission added. Thanks to anyone that takes the time to help.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

You have an error in your PHP code, it should be:

<?php 
    $name = urldecode($_GET['name']);
    $user = urldecode($_GET['user']);
    $email = urldecode($_GET['email']);
    $pass = urldecode($_GET['pass']);
    echo 'Name ' . $name . 'Email: ' . $email . ' User' . $user . ' Pass  :' . $pass;

You are missing . between each of the string concatenations. If this still does not help, please prepend: error_reporting(E_ALL) and let us know what is the error when you are trying to reach the script on your webserver.

Krac
  • 146
  • 6
  • Notice: Undefined index: name in C:\xampp\htdocs\kb_test\ejemplo.php on line 3 Notice: Undefined index: user in C:\xampp\htdocs\kb_test\ejemplo.php on line 4 Notice: Undefined index: email in C:\xampp\htdocs\kb_test\ejemplo.php on line 5 Notice: Undefined index: pass in C:\xampp\htdocs\kb_test\ejemplo.php on line 6 – Philip J Fry Apr 11 '16 at 14:56
  • Seems like you are using get method to pass the params. Instead `$_POST` use `$_GET` and all should be good. I updated code for you. Check my answer once again. – Krac Apr 11 '16 at 15:08
  • Wel you are only sending the name parameter. No wonder that the script complains about missing user, email and pass. Use the isset() function before you use a parameter. Check if it exists. – greenapps Apr 12 '16 at 15:28
  • If you notice the rest of the parameters are between "/*" to make the tests easier. The isset you're suggesting has been tried before, but it wasn't the error source. It is like it connects with the php but the parameter it sends it's empty , because teh echo back its just the echo tag. – Philip J Fry Apr 13 '16 at 13:42
0

Nonsense that you have no LogCat. Of course there is one if you have connected your device to your pc.

You cannot call getText() in an onClick() handler. As all network code has to be done in an AsyncTask or thread.

Thats why you have a NetworkOnMainThreadException now.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Thanks for the advice , i've already changed my code to match the correct ways of retrieving text from the GUI, but i still have the same problem. And no , i don't have a LogCat. I only can test the code by building and apk and then transferring to a real device. – Philip J Fry Apr 12 '16 at 14:38
  • What do you mean by transferring to a real device? Normally you would connect your device with an usb cable to your pc. And the IDE (Eclipse or Android Studio) would upload your app to the device. And let it run. And of course both IDEs have a LogCat where you can follow whats happening. – greenapps Apr 12 '16 at 14:42
  • Correct ways of retrieving text from the gui? Where are you talking about? We spoke about the correct way to do internet connections. In a thread or AsyncTask. Now do you use one of them now? – greenapps Apr 12 '16 at 14:45
  • Once again , i am telling you i don't have the device connected via adb. My pc does not support either emulators or debbuging with mobile. And yes, with the getText() i was retrieving a label from the GUI in an incorrect place where later i would send it by the urlconnection – Philip J Fry Apr 12 '16 at 15:02