栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Android JSON至PHP Server并返回

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android JSON至PHP Server并返回

好的,我有PHP。下面检索POST ed数据并返回服务

<?php$data = file_get_contents('php://input');$json = json_depre($data);$service = $json->{'service'};print $service;?>

和Android代码:

在onCreate()中

path = "http://example.com/process/json.php";    HttpClient client = new DefaultHttpClient();    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout          // Limit    HttpResponse response;    JSonObject json = new JSonObject();    try {        HttpPost post = new HttpPost(path);        json.put("service", "GOOGLE");        Log.i("jason Object", json.toString());        post.setHeader("json", json.toString());        StringEntity se = new StringEntity(json.toString());        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,     "application/json"));        post.setEntity(se);        response = client.execute(post);                if (response != null) { InputStream in = response.getEntity().getContent(); // Get the         // data in  // the  // entity String a = convertStreamToString(in); Log.i("Read from Server", a);        }    } catch (Exception e) {        e.printStackTrace();    }

还有你想去的地方

private static String convertStreamToString(InputStream is) {    BufferedReader reader = new BufferedReader(new InputStreamReader(is));    StringBuilder sb = new StringBuilder();    String line = null;    try {        while ((line = reader.readLine()) != null) { sb.append(line + "n");        }    } catch (IOException e) {        e.printStackTrace();    } finally {        try { is.close();        } catch (IOException e) { e.printStackTrace();        }    }    return sb.toString();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595116.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号