ds2321 2015-09-23 13:55
浏览 48
已采纳

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

I have a php file that generates data that must take to Android.

This is the file output .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"}]

Now there have been changes with the class apache: Link here

Looking around I find several guides on the old method but I wanted to use the new one since my app is the targetSdkVersion 23.
I think this is the new method with JsonReader and HttpURLConnection.

I tried but I can not make it work or understand how to handle it.

So I ask, how do I take the strings from php page that creates? (example: title and link)

  • 写回答

1条回答 默认 最新

  • douxing1969 2015-09-23 19:26
    关注

    please try this.

    it's working for me.

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

    in this case i was using GSON to later parse the response but you can parse it as you wish.

    this is a snippet of code that i can provide, if you need more help or if some dependencies are missing please tell me so i can point you in the right direction.

    have a look at this tutorial for more help

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

    if you need a webservice PHP framework, take a look at this

    http://www.slimframework.com/

    it's very easy to use and ligthweight framework so you can use it as a service to send and recieve info from your server

    hope it helps, feel free to ask if you need any more help

    cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配