Bojje Bojje - 2 months ago 14
PHP Question

Why can't I read php echo or printf from android app?

I'm trying to write on my android app just a small message from a php-file I have on my WAMP-server.

In my main activity I am only using a button to create a

AsyncTask
that should find the php-file and read the content of it and print it out in the form of a
Toast
- message.

<Button
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="@string/log_in"
android:id="@+id/logInButton"
android:layout_below="@+id/passwordText"
android:layout_marginTop="27dp"
android:layout_alignLeft="@+id/registerText"
android:layout_alignStart="@+id/registerText"
android:layout_alignRight="@+id/passwordText"
android:layout_alignEnd="@+id/passwordText"
android:layout_alignParentEnd="false"
android:layout_alignParentStart="false"
android:onClick="LogIn" />


And the function in my main class

public void LogIn (View view){
//String username = _userName.getText().toString();
//String password = _password.getText().toString();
BackgroundHandler bh = new BackgroundHandler(this);
bh.execute();
/*FetchPhp fp = new FetchPhp(this);
String m = fp.GetTheFuckingEcho();
Toast.makeText(this, m , Toast.LENGTH_SHORT);*/
}


And here is Background handler that should fetch the php echo.

public class BackgroundHandler extends AsyncTask<String, Void, String> {
Context c;
AlertDialog alert;
String line , results;

public BackgroundHandler(Context c){this.c = c; }

@Override
protected String doInBackground(String...args) {
try {
String db_url = "http://192.168.1.210:8081/test.php";

URL url = new URL(db_url);
HttpURLConnection httpHandler = (HttpURLConnection) url.openConnection();
httpHandler.setRequestMethod("GET");
httpHandler.setDoInput(true);
InputStream is = httpHandler.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
results = br.readLine();
} catch(Exception e){
Log.d("ErrorMessage", e.getLocalizedMessage());
}
return null;
}

@Override
protected void onPostExecute(String s) {
Toast.makeText(c, results, Toast.LENGTH_SHORT);
}

@Override
protected void onPreExecute() {
// alert = new AlertDialog.Builder(c).create();
// alert.setTitle("TestTitle");
}

}


When I click on the button NOTHING happends. I've try with several different approaches and nothing is working. And I'm not receiving any error messages or problems. Nothing happends. And yes I have

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


in my Manifest.xml-File. and I can access the php-file through my local and "distant" computers. I just can't seem to fetch the echo string.

And the php-file contains
<?php printf("test"); ?>


Edit here is the full MainActivity class

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

Button _logIn;
EditText _userName;
EditText _password;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

_logIn = (Button) findViewById(R.id.logInButton);
_userName = (EditText) findViewById(R.id.usernameText);
_password = (EditText) findViewById(R.id.passwordText);
CheckForNTConnection();
}

public void LogIn (View view){
//String username = _userName.getText().toString();
//String password = _password.getText().toString();
BackgroundHandler bh = new BackgroundHandler(this);
bh.execute();

/*FetchPhp fp = new FetchPhp(this);
String m = fp.GetTheFuckingEcho();
Toast.makeText(this, m , Toast.LENGTH_SHORT);*/
}
//NT = Network
public void CheckForNTConnection()
{
ConnectivityManager CM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = CM.getActiveNetworkInfo();
if(info != null && info.isConnected())
{
Toast.makeText(this, info.getTypeName(), Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(this, "Some error", Toast.LENGTH_SHORT).show();
}
}
}

Answer
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    _logIn = (Button) findViewById(R.id.logInButton);
    _userName = (EditText) findViewById(R.id.usernameText);
    _password = (EditText) findViewById(R.id.passwordText);
    CheckForNTConnection();
    LogIn();
}

public void LogIn (){

    BackgroundHandler bh = new BackgroundHandler(this);
    bh.execute();

}
//NT = Network
public void CheckForNTConnection()
{
    ConnectivityManager CM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = CM.getActiveNetworkInfo();
    if(info != null && info.isConnected())
    {
        Toast.makeText(this, info.getTypeName(), Toast.LENGTH_SHORT).show();
    } else
    {
        Toast.makeText(this, "Some error", Toast.LENGTH_SHORT).show();
    }
}
}

public class BackgroundHandler extends AsyncTask<String, Void, String> {
Context c;
AlertDialog alert;


public BackgroundHandler(Context c)
{this.c = c; 
}

@Override
protected String doInBackground(String...args) {
    String line , results;
    try {
        String db_url = "http://192.168.1.210:8081/test.php";

        URL url = new URL(db_url);
        HttpURLConnection httpHandler = (HttpURLConnection) url.openConnection();
        httpHandler.setRequestMethod("GET");
        httpHandler.setDoInput(true);
        InputStream is = httpHandler.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        results = br.readLine();
    } catch(Exception e){
        Log.d("ErrorMessage", e.getLocalizedMessage());
    }
    return results;
}

@Override
protected void onPostExecute(String s) {
    Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); //result is s
}

@Override
protected void onPreExecute() {
   // alert = new AlertDialog.Builder(c).create();
   // alert.setTitle("TestTitle");
}

}