为什么我无法从 android 应用读取 php echo 或 printf?

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

我正在尝试在我的 android 应用程序上写一条来自我的 WAMP 服务器上的 php 文件的小消息。

在我的主 activity 中,我只使用一个按钮来创建一个 AsyncTask,它应该找到 php 文件并读取它的内容并在Toast- 消息的形式。

<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" />

还有我的主函数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);*/
}

这是应该获取 php 回显的后台处理程序。

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");
}

}

当我点击按钮时没有任何反应。我尝试了几种不同的方法,但没有任何效果。而且我没有收到任何错误消息或问题。什么都没发生。是的,我有

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

在我的 Manifest.xml- 文件中。我可以通过我的本地和 "distant" 计算机访问 php-文件。我似乎无法获取回显字符串。

并且 php 文件包含 <?php printf("test"); ?>

这里编辑的是完整的MainActivityclass

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();
        }
    }
}
@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");
}

}