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

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

jsc9年前 (2016-04-06)Android3488
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发送数据到服务器” 的相关文章

android json解析及简单例子

JSON的定义:       一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式...

创建Popwindow弹出菜单的两种方式

创建Popwindow弹出菜单的两种方式

方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.v...

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

注:(图中每一个条目和图标都是由代码动态生成) 代码动态布局,并需要为每一个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon" ...

Android中从SD卡中/拍照选择图片并进行剪裁的方法

Android中从SD卡中/拍照选择图片并进行剪裁的方法

效果图: 下面是代码的部分,部分是从网路上摘录的,自己整理后当做工具类使用   配置文件:布局很简单,一个ImageButton和一个Button,点击都可以实现图像选择的功能,具体的实现根据大家在实际中...

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

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

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

GridView中item高度自适应

item高度自适应public class MyAdapter extends BaseAdapter {         GridView mGv;&n...