您的位置:首页 > Web前端 > JavaScript

从安卓客户端向服务器端发送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;

/*
* 从安卓浏览器向服务器端发送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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: