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.