从安卓客户端向服务器端发送JSON格式的数据

1.在客户端获取到用户输入的信息数据;

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);
	}
}



  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值