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

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

jsc9年前 (2016-04-06)Android3525
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中Parcelable接口用法

1. Parcelable接口Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface m...

Fragment生命周期

Fragment生命周期

onAttach方法:Fragment和Activity建立关联的时候调用。onCreateView方法:为Fragment加载布局时调用。onActivityCreated方法:当Activity中的onCreate方法执行完后调用。onDestroyView方法:Fragment中的布局被移除时...

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

    了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候,老板说要加上二维码扫描功能,然后自己的屁颠屁颠的去百度,google...

android json解析及简单例子

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

GridView中item高度自适应

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

Android SharedPreferences PreperenceScreen 偏好数据存取

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