Android使用JSON想服务器发送数据
2014-03-27 08:43
316 查看
final String urlPath="http://60.123.36.163:8080/wms/resisteruser.do";
URL url;
try
{
url = new URL(urlPath);
/*封装子对象*/
JSONObject ClientKey = new JSONObject();
ClientKey.put("appusername", userName.getText().toString());
ClientKey.put("eigenvalues", passWord.getText().toString());
ClientKey.put("telephone", phoneNum.getText().toString());
ClientKey.put("apprealname", realName.getText().toString());
ClientKey.put("email", emailNum.getText().toString());
ClientKey.put("sex", sex);
/*封装Person数组*/
JSONObject params = new JSONObject();
params.put("Person", ClientKey);
/*把JSON数据转换成String类型使用输出流向服务器写*/
String content = String.valueOf(params);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);//设置允许输出
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Fiddler");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
/*服务器返回的响应码*/
int code = conn.getResponseCode();
if(code == 1)
{
Toast.makeText(Register.this, "恭喜您注册成功!", Toast.LENGTH_SHORT).show();
Intent intentToLogin=new Intent();
intentToLogin.setClass(Register.this,Login.class);
startActivity(intentToLogin);
}
}
catch (Exception e)
{
e.printStackTrace();
}
URL url;
try
{
url = new URL(urlPath);
/*封装子对象*/
JSONObject ClientKey = new JSONObject();
ClientKey.put("appusername", userName.getText().toString());
ClientKey.put("eigenvalues", passWord.getText().toString());
ClientKey.put("telephone", phoneNum.getText().toString());
ClientKey.put("apprealname", realName.getText().toString());
ClientKey.put("email", emailNum.getText().toString());
ClientKey.put("sex", sex);
/*封装Person数组*/
JSONObject params = new JSONObject();
params.put("Person", ClientKey);
/*把JSON数据转换成String类型使用输出流向服务器写*/
String content = String.valueOf(params);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);//设置允许输出
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Fiddler");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
/*服务器返回的响应码*/
int code = conn.getResponseCode();
if(code == 1)
{
Toast.makeText(Register.this, "恭喜您注册成功!", Toast.LENGTH_SHORT).show();
Intent intentToLogin=new Intent();
intentToLogin.setClass(Register.this,Login.class);
startActivity(intentToLogin);
}
}
catch (Exception e)
{
e.printStackTrace();
}
相关文章推荐
- Android使用HttpPost向服务器发送Json数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpPost向服务器发送Json数据
- Android使用HttpPost向服务器发送Json数据
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- zz:Android使用HttpPost向服务器发送Json数据
- Android使用HttpPost向服务器发送Json数据
- Android实战:使用Volley框架访问服务器Json数据
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- Android使用HttpUrlConnection请求服务器发送数据详解
- 使用curl库,以post方式向服务器发送json/字符串数据
- 使用curl库,以post方式向服务器发送json数据
- Ajax---发送参数:使用JSON替代XML向服务器发送数据
- Android与Java 服务器使用Socket协议实现Json数据传输
- Android,使用Json发送数据中,使用的Java转义字符 KanKan原创
- Android利用JSON发送数据到服务器
- android发送json数据到服务器(框)
- 使用JSON向服务器发送数据