当前位置:首页 > Android > 正文内容

Android利用JSON发送数据到服务器

jsc9年前 (2016-04-06)Android3670
new Thread()
{


@Override
public void run() {
// TODO Auto-generated method stub
Looper.prepare(); 
final String urlPath="http://60.176.36.125:8080/wms/resisteruser.do";
URL url;
try 
{
url = new URL(urlPath);
/*封装子对象*/
JSONObject ClientKey = new JSONObject();
ClientKey.put("appusername", userName.getText().toString());
ClientKey.put("passwd", passWord.getText().toString());
ClientKey.put("eigenvalues", eigenValues.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");
conn.setRequestProperty("Charset", encoding); 
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
/*服务器返回的响应码*/
int code = conn.getResponseCode();
if(code == 200)
{
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
String retData = null;
String responseData = "";
while((retData = in.readLine()) != null)
{
responseData += retData;
}
JSONObject jsonObject = new JSONObject(responseData);
JSONObject succObject = jsonObject.getJSONObject("regsucc");
//System.out.println(result);
String success = succObject.getString("id");

in.close();
//System.out.println(success);
Toast.makeText(Register.this, success, Toast.LENGTH_SHORT).show();
Intent intentToLogin=new Intent();
intentToLogin.setClass(Register.this,Login.class);
startActivity(intentToLogin);
finish();
}
else 
{
Toast.makeText(getApplicationContext(), "数据提交失败", Toast.LENGTH_SHORT).show();
}
} 
catch (Exception e) 
{
// TODO: handle exception
throw new RuntimeException(e);
}
Looper.loop(); 
}

}.start();


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/69.html

标签: JSON
分享给朋友:

“Android利用JSON发送数据到服务器” 的相关文章

TypedArray和obtainStyledAttributes使用

TypedArray和obtainStyledAttributes使用

在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutton.TextButto...

Android 学习之 开源项目PullToRefresh的使用

Android 学习之 开源项目PullToRefresh的使用

首先 下载 Android-PullToRefresh-master 下载地址  https://github.com/chrisbanes/Android-PullToRefresh 下载之...

Fragment保持状态切换

Fragment保持状态切换

在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常挂掉网上说使用replace切换会使被切换的Fragment给替换掉,从而被被切换的Fra...

Android中的消息通知(NotificationManager和Notification)

下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提 示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个快讯。已添加的Notification.Builder,使其更容易构建通知。 notifica...

Android SharedPreferences PreperenceScreen 偏好数据存取

SharedPreferences是一个接口,程序是无法创建SharedPreferences实例的,1、Context.getSharedPreferences(String name,int mode)来得到一个指定name的SharedPreferences实例2、PreferenceMana...

获取Android手机中SD卡存储信息

SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件。因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android开发中,我们如何能获取SD卡的内存容量呢?首先,要获取SD卡上面的信息,必须先对SD卡有访问的权限,...