从 php json 获取字符串编码为 android 目标 api23+

get string from php json encode to android target api23+

我有一个 php 文件,它生成必须传送到 Android 的数据。

这是文件输出。php

[{"item":"1","title":"Title of one","link":"qwerty1234"},{"item":"2","title":"Title of two","link":"qwerty1234"},{"item":"3","title":"Title of three","link":"qwerty1234"}]

现在 class apache 发生了变化:Link here

环顾四周,我发现了一些关于旧方法的指南,但我想使用新方法,因为我的应用程序是 targetSdkVersion 23。
我认为这是 JsonReader 和 HttpURLConnection 的新方法。

我试过了,但我无法让它工作或理解如何处理它。

所以我问,如何从创建的 php 页面中获取字符串? (例如:标题和 link)

请试试这个。

它对我有用。

    String url = "someurl.php";
    HttpPost httppost = new HttpPost(url);

    // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();

    try {
        HttpResponse response = httpClient.execute(httpPost);
        String responseString = EntityUtils.toString(response.getEntity());

        JSONObject jsonObject = new JSONObject(responseString);

        String error = jsonObject.getString("item1");

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

在这种情况下,我使用 GSON 稍后解析响应,但您可以根据需要解析它。

这是我可以提供的代码片段,如果您需要更多帮助或缺少某些依赖项,请告诉我,以便我为您指明正确的方向。

查看本教程以获得更多帮助

http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php

如果你需要一个网络服务 PHP 框架,看看这个

http://www.slimframework.com/

它是非常易于使用的轻量级框架,因此您可以将它用作服务来从您的服务器发送和接收信息

希望对您有所帮助,如果您需要更多帮助,请随时询问

干杯