从安卓客户端向服务器端发送JSON格式的数据
2015-08-13 20:22
579 查看
1.在客户端获取到用户输入的信息数据;
2.强该数据包装成JSON格式的数据流;
3.将JSON格式的数九流转化成String,使用输出流写到服务器中。
JSONARRAY/JSONObject http(post)
JavaBean ---------------------------------------> JSON -----------------> WEB服务器
客户端代码如下 MainActivity.java主要用来获取从界面上用户输入的信息,并把该信息封装成JSON格式的数据,再发送出去,用到了一个简单的Person类person.java;
2.强该数据包装成JSON格式的数据流;
3.将JSON格式的数九流转化成String,使用输出流写到服务器中。
JSONARRAY/JSONObject http(post)
JavaBean ---------------------------------------> JSON -----------------> WEB服务器
客户端代码如下 MainActivity.java主要用来获取从界面上用户输入的信息,并把该信息封装成JSON格式的数据,再发送出去,用到了一个简单的Person类person.java;
/* * 从安卓浏览器向服务器端发送Json数据 * */ public class MainActivity extends Activity { private Button submit = null; URL url = null; String urlPath = "http://10.0.3.2:8080/XMLParse/AcceptJsonServlet"; EditText name = null; EditText age = null; EditText address = null; Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x123) { Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(MainActivity.this, "发送失败", Toast.LENGTH_LONG) .show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = ((EditText) findViewById(R.id.name)); age = ((EditText) findViewById(R.id.age)); address = ((EditText) findViewById(R.id.address)); submit = (Button) findViewById(R.id.submit); submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread() { public void run() { JSONObject js = new JSONObject(); JSONObject params = new JSONObject(); // JSONArray array = new JSONArray(); Person p = new Person(name.getText().toString(), age .getText().toString(), address.getText() .toString()); // 封装子对象 try { js.put("name", p.getName()); js.put("age", p.getAge()); js.put("address", p.getAddress()); // 封装Person数组 params.put("Person", js); } catch (JSONException e) { e.printStackTrace(); } // 把Json数据转换成String类型,使用输出流向服务器写 final String content = String.valueOf(params); try { url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url .openConnection(); conn.setConnectTimeout(5000); conn.setDoOutput(true);// 设置允许输出 conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); // 内容类型 OutputStream os = conn.getOutputStream(); os.write(content.getBytes()); os.close(); if (conn.getResponseCode() == 200) { handler.sendEmptyMessage(0x123); } else { handler.sendEmptyMessage(0x122); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); // noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
相关文章推荐
- 深入JavaScript(15)JavaScript核心(晋级高手必读篇)
- Hive JSON数据处理的一点探索
- 深入JavaScript(14)JavaScript与DOM(下)
- 深入JavaScript(13)JavaScript与DOM(上)
- JSON Web Token (JWT) 简介
- 深入JavaScript(11)求值策略(Evaluation strategy)
- 深入JavaScript(10)面向对象编程之概论
- 深入JavaScript(9)函数
- 深入JavaScript(8)S.O.L.I.D五大原则之里氏替换原则LSP
- 深入JavaScript(7)S.O.L.I.D五大原则之开闭原则OCP
- 深入JavaScript(6)S.O.L.I.D五大原则之单一职责SRP
- 深入JavaScript(5)强大的原型和原型链
- angualrJS中的坑
- 调试时屏蔽JavaScript库代码 –Chrome DevTools Blackbox功能介绍
- 原生JavaScript addClass() removeClass()
- js参数传值--中文乱码
- js参数传值--中文乱码
- js参数传值--中文乱码
- js 对有“命名空间”的表单做深度解析
- js-处理div设置的编辑框处理焦点定位追加内容