Android 网络服务 php

Android Webservices with php

我想使用 php 代码创建 Web 服务,PHP 包含文本的文件,我想显示此文本 android textVeiw

我通过 openConnection() 与 URL 连接,但我在 class InputStreamReader

中出错
public class MainActivity extends AppCompatActivity {
    TextView text;

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

        text = (TextView) findViewById(R.id.textView4);    
    }    

    public void onClick(View view) throws Exception {    

        URL hellofileurl = null;
        try {
            hellofileurl = new URL("http://waaddev.ahosti.net/web.php");
            HttpURLConnection myfirstconnection = (HttpURLConnection) hellofileurl.openConnection();
            InputStream stream = new InputStream(myfirstconnection.getInputStream());//i have error in this line 
            BufferedReader b = new BufferedReader(stream);
            String hello = b.readLine();    

        } catch (Exception e) {
            e.printStackTrace();
        }    
    }    
}

InputStream class 已被弃用。

您可以使用:

    URL url = new URL("http://waaddev.ahosti.net/web.php");
    URLConnection urlConnection = url.openConnection();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

    String line;
    while ((line = bufferedReader.readLine()) != null)
    {
         content.append(line + "\n");
    }
    bufferedReader.close();
    String hello = content.toString();

您可以查看我的存储库,这是一个清楚的示例,说明如何对 PHP 网络服务进行简单调用(还有 MySQL)。 在此示例中,您在远程 MySQL 数据库中有一个产品列表,您可以通过 PHP Web 服务将它们获取到您的应用程序中。

您还可以通过您的应用程序将项目添加到数据库中。 希望能解决你的疑惑。

存储库是: https://github.com/alessandroargentieri/AndroidToWebService