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 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错