好的,我有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();}


