【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1765|回复: 0

Android中使用HttpPost方式提交到服务器端并通过PHP Json返回数据

[复制链接]
发表于 2013-1-15 23:26:34 | 显示全部楼层 |阅读模式
在上篇中使用HttpGet获取服务器数据后,本文我们使用HttpPost的方式来提交数据。稍微有一点不同的就是需要使用BasicNameValuePair来放一些Post的数据。
在访问web页面的数据
{“title”:”未输入”,”id”:1,”value”:”7030ff64701a938becbc5aa67ddb86e8″}

—————————————————————————————
先看php代码

  1. < ?php
  2.         header("Content-Type: text/html; charset=UTF-8");
  3.         if(isset($_POST['username']) && isset($_POST['password']))
  4.         {
  5.                 $username = $_POST['username'];
  6.                 $password = $_POST['password'];
  7.                 if($username == "huzhangyou" && $password == "windows")
  8.                 {
  9.                         $array = array( 'title'=>urlencode('登陆成功'), 'id'=>1, 'value'=>md5(md5($username. $password)));
  10.                         echo urldecode(json_encode($array));
  11.                 }
  12.                 else
  13.                 {
  14.                         $array = array( 'title'=>urlencode('登录失败'), 'id'=>1, 'value'=>md5("错误"));
  15. echo urldecode(json_encode($array));
  16.                 }
  17.         }
  18.         else
  19.         {
  20.                 $array = array( 'title'=>urlencode('未输入'), 'id'=>1, 'value'=>md5("错误"));
  21. echo urldecode(json_encode($array));
  22.         }
  23. ?>
复制代码
—————————————————————————————
java部分核心代码:

  1. public void postServerJsonDataWithNoType(String url,EditText editText)
  2.     {
  3.             HttpPost httpPost = new HttpPost(url);
  4.             HttpClient client = new DefaultHttpClient();
  5.             StringBuilder str = new StringBuilder();
  6.             ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
  7.             params.add(new BasicNameValuePair("username","huzhangyou"));
  8.             params.add(new BasicNameValuePair("password","windows"));
  9.             BufferedReader buffer = null;
  10.             try
  11.             {
  12.                     httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
  13.                     HttpResponse httpRes = client.execute(httpPost);
  14.                     if(httpRes.getStatusLine().getStatusCode() == 200)
  15.                     {
  16.                             buffer = new BufferedReader(new InputStreamReader(httpRes.getEntity().getContent()));
  17.                             for(String s = buffer.readLine(); s != null ; s = buffer.readLine())
  18.                             {
  19.                                     str.append(s);
  20.                             }
  21.                             //String out = EntityUtils.toString(httpRes.getEntity().getContent(), "UTF-8");
  22.                             //StringBuilder sb = new StringBuilder()
  23.                             Log.i(Tag,str.toString());
  24.                             buffer.close();
  25.                             JSONObject json = new JSONObject(str.toString());
  26.                                 String title = json.getString("title");
  27.                                 Log.i(Tag,title);
  28.                                 int id = json.getInt("id");
  29.                                 String value = json.getString("value");
  30.                                 Log.i(Tag,value);
  31.                                 editText.setText("Title:" + title + " ID:" + id + " Value:" + value);
  32.                     }
  33.             }
  34.             catch(Exception e)
  35.             {
  36.                     if(buffer != null)
  37.                     {
  38.                             try {
  39.                                         buffer.close();
  40.                                 } catch (IOException e1) {
  41.                                         // TODO Auto-generated catch block
  42.                                         e1.printStackTrace();
  43.                                 }
  44.                     }
  45.             }
  46.     }
复制代码
—————————————————————————————
项目代码下载:
php:
phpjson
java代码:
PHPJson
1 http://blog.csdn.net/firewings_r/article/details/5374851
2 http://www.ineeke.com/archives/1317/
3 http://lrc-1986.iteye.com/blog/868799
4 http://stackoverflow.com/questio ... w-to-get-the-result
5 http://stackoverflow.com/questio ... ttp-post-in-android
6 http://www.anddev.org/doing_http_post_with_android-t492.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-5-6 19:43

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表