Android 如何发送多个json对象';安卓系统中的服务器数据?

Android 如何发送多个json对象';安卓系统中的服务器数据?,android,json,http-post,server,Android,Json,Http Post,Server,如何向服务器发送多个json值 public class UserVO { private String userId; private String firstName; private String lastName; private String mobileNumber; private String stdCode; private String landlineNumber; private String coun

如何向服务器发送多个json值

    public class UserVO {
    private String userId;
    private String firstName;
    private String lastName;   
    private String mobileNumber;
    private String stdCode;
    private String landlineNumber;
    private String countryCode;
    private String stateCode;
    private String zipcode;
    private String status;    
    private LoginVO loginVO;
    private AddressVO addressVO;
    private proofVO proofVO VO;
}
我是这样实现的

next_Edit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new CreateUser().execute();

        }
    });

class CreateUser extends AsyncTask<String, String, String> {
    int success=0;
    JSONObject json;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ConsentActivity.this);
        pDialog.setMessage("Registering New User..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {

        SharedPreferences pref = getSharedPreferences("UserMgmt", Context.MODE_PRIVATE);

        String Firstname = pref.getString("FName", "");
        String Lastname = pref.getString("LName", "");
        String Userid = pref.getString("UserId", "");
        String Password = pref.getString("Password", "");
        String Repassword = pref.getString("RePassword", "");

        String Idproof_typ=pref.getString("IdType","");
        String Idproof_number=pref.getString("IdNumber","");
        String Dob=pref.getString("DOB","");
        String Gender=pref.getString("Gender","");
        String Email=pref.getString("Email","");
        String Mobile_number=pref.getString("Mobile","");
        String Country_code=pref.getString("CountryCode","");
        String Landline=pref.getString("LandLine","");

        String Country=pref.getString("Country","");
        String State=pref.getString("State","");
        String Address=pref.getString("Address","");
        String Pincod=pref.getString("Pincode","");

        boolean Tc_chk_state = pref.getBoolean("ChkBoxTC", true);
        String status_tc_chk=String.valueOf(Tc_chk_state);
        String status_tc=status_tc_chk.substring(0,1);

        boolean Consent_chk_state = pref.getBoolean("ChkBoxConsent", true);
        String status_con_chk=String.valueOf(Tc_chk_state);
        String status_con=status_con_chk.substring(0,1);

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("userId", Userid));
        params.add(new BasicNameValuePair("firstName", Firstname));
        params.add(new BasicNameValuePair("lastName", Lastname));
        params.add(new BasicNameValuePair("identityType", Idproof_typ));
        params.add(new BasicNameValuePair("identityNumber", Idproof_number));
        params.add(new BasicNameValuePair("dob", Dob));
        params.add(new BasicNameValuePair("gender", Gender));
        params.add(new BasicNameValuePair("emailId", Email));
        params.add(new BasicNameValuePair("mobileNumber", Mobile_number));
        params.add(new BasicNameValuePair("stdCode", Country_code));
        params.add(new BasicNameValuePair("landlineNumber", Landline));
        params.add(new BasicNameValuePair("countryCode", Country));
        params.add(new BasicNameValuePair("stateCode", State));
        params.add(new BasicNameValuePair("zipcode", Pincod));
        params.add(new BasicNameValuePair("status", status_tc));
        params.add(new BasicNameValuePair("password", Password));
        params.add(new BasicNameValuePair("address", Address));


       /*json = jsonParser.makeHttpRequest(url_new_user,
                "POST",params);*/
              //  Log.d("Create Response", json.toString());

        JSONArray jsonArray = new JSONArray();
        final  JSONObject json = new JSONObject();
        final JSONObject login = new JSONObject();
        final JSONObject address = new JSONObject();
        final JSONObject identity = new JSONObject();
        HttpResponse response;
        HttpClient client = new DefaultHttpClient();
        HttpContext httpContext = new BasicHttpContext();
        HttpPost post = new HttpPost("http://191.121.1.145:8080/medicine/services/users/createUser");
        post.setHeader("Content-type", "application/json");
        try {

            Log.e("userId",Userid);
            Log.e("firstName",Firstname);
            Log.e("lastName",Lastname);
            Log.e("emailId",Email);
            Log.e("password",Password);
            Log.e("newPassword",Repassword);
            Log.e("status",status_tc);


            json.put("userId", Userid);
            json.put("firstName", Firstname);
            json.put("lastName", Lastname);
            //json.put("identityType",Idproof_typ);
            //json.put("identityNumber",Idproof_number);
            json.put("dob",Dob);
            json.put("gender",Gender);
            json.put("emailId", Email);
            json.put("mobileNumber",Mobile_number);
            json.put("stdCode",Country_code);
            json.put("landlineNumber",Landline);
            json.put("countryCode",Country);
            json.put("stateCode",State);
            json.put("zipcode",Pincod);
            json.put("status",status_tc);
            //json.put("password",Password);
           // json.put("address",Address);
            jsonArray.put(json);
            try{
                login.put("password",Password);
                jsonArray.put(login);
            }catch(Exception exception){
                exception.printStackTrace();
            }


            try{
                address.put("address",Address);
                jsonArray.put(address);
            }catch(Exception exception){
                exception.printStackTrace();
            }


            try{
                identity.put("identityType",Idproof_typ);
                identity.put("identityNumber",Idproof_number);
                jsonArray.put(identity);
            }catch(Exception exception){
                exception.printStackTrace();
            }

            StringEntity se = new StringEntity( jsonArray.toString());
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(se);
            response = client.execute(post);
            HttpEntity entity = response.getEntity();
            if (response.getStatusLine().getStatusCode() == 201){
                Log.e("Responce","OK");
            }
        }
        catch(Exception e) {
            e.printStackTrace();
            //Toast.makeText(getApplicationContext(), "establishment with server failed", Toast.LENGTH_LONG).show();
            // createDialog("Error", "Cannot Estabilish Connection");
        }
        /*try {
            success = json.getInt(TAG_SUCCESS);


        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }*/



        return null;
    }

   protected void onProgressUpdate(Integer... progress) {

   }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();

            /* try {
                int success = json.getInt(TAG_SUCCESS);*/

                if (success == 1) {
                // successfully created a user
                Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);
                // closing this screen
                finish();
               } /*else {
                // failed to create user
              //  Log.d("failed to create user", json.toString());

                 }*/
        /*} catch (JSONException e) {
            e.printStackTrace();
        }*/

    }

}
next\u Edit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新建CreateUser().execute();
}
});
类CreateUser扩展异步任务{
int成功=0;
JSONObject json;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新的ProgressDialog(同意Activity.this);
pDialog.setMessage(“注册新用户…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…args){
SharedReferences pref=getSharedReferences(“UserMgmt”,Context.MODE\u PRIVATE);
String Firstname=pref.getString(“FName”,”);
String Lastname=pref.getString(“LName”,即“”);
String Userid=pref.getString(“Userid”,”);
String Password=pref.getString(“Password”,“Password”);
字符串Repassword=pref.getString(“Repassword”和“”);
字符串Idproof_typ=pref.getString(“IdType”,”);
字符串Idproof_number=pref.getString(“IdNumber”,即“”);
字符串Dob=pref.getString(“Dob”,即“”);
String-Gender=pref.getString(“Gender”,”);
String Email=pref.getString(“Email”,”);
String Mobile_number=pref.getString(“Mobile”,“Mobile”);
字符串Country_code=pref.getString(“CountryCode”,”);
String Landline=pref.getString(“Landline”,“Landline”);
String Country=pref.getString(“国家”,“国家”);
String State=pref.getString(“State”,”);
String Address=pref.getString(“Address”,”);
String Pincod=pref.getString(“Pincode”,即“);
布尔Tc_chk_state=pref.getBoolean(“ChkBoxTC”,true);
字符串状态\u tc\u chk=String.valueOf(tc\u chk\u状态);
字符串状态\u tc=状态\u tc\u chk.子字符串(0,1);
布尔同意_chk_state=pref.getBoolean(“ChkBoxConsent”,true);
String status\u con\u chk=String.valueOf(Tc\u chk\u state);
字符串状态\u con=状态\u con\u chk.子字符串(0,1);
List params=new ArrayList();
添加(新的BasicNameValuePair(“userId”,userId));
参数add(新的BasicNameValuePair(“firstName”,firstName));
添加(新的BasicNameValuePair(“lastName”,lastName));
参数添加(新的BasicNameValuePair(“identityType”,Idproof_typ));
参数add(新的BasicNameValuePair(“identityNumber”,Idproof_编号));
参数添加(新的BasicNameValuePair(“dob”,dob));
参数添加(新的BasicNameValuePair(“性别”,性别));
参数add(新的BasicNameValuePair(“emailId”,Email));
参数add(新的BasicNameValuePair(“mobileNumber”,Mobile_编号));
参数添加(新的BasicNameValuePair(“标准代码”,国家/地区代码));
参数添加(新的BasicNameValuePair(“座机号码”,座机号码));
参数添加(新的BasicNameValuePair(“国家代码”,国家));
参数add(新的BasicNameValuePair(“stateCode”,State));
添加参数(新的BasicNameValuePair(“zipcode”,Pincod));
参数添加(新的BasicNameValuePair(“状态”,status_tc));
添加(新的BasicNameValuePair(“密码”,password));
参数add(新的BasicNameValuePair(“地址”,地址));
/*json=jsonParser.makeHttpRequest(url\u new\u user,
“POST”,params)*/
//d(“创建响应”,json.toString());
JSONArray JSONArray=新的JSONArray();
final JSONObject json=new JSONObject();
最终JSONObject登录=新建JSONObject();
最终JSONObject地址=新JSONObject();
最终JSONObject标识=新JSONObject();
HttpResponse响应;
HttpClient=new DefaultHttpClient();
HttpContext HttpContext=新的BasicHttpContext();
HttpPost=新的HttpPost(“http://191.121.1.145:8080/medicine/services/users/createUser");
setHeader(“内容类型”、“应用程序/json”);
试一试{
Log.e(“userId”,userId);
Log.e(“名字”,名字);
Log.e(“lastName”,lastName);
Log.e(“emailId”,Email);
Log.e(“密码”,password);
Log.e(“新密码”,重新密码);
Log.e(“状态”,状态_tc);
put(“userId”,userId);
json.put(“firstName”,firstName);
put(“lastName”,lastName);
//json.put(“identityType”,Idproof_-typ);
//json.put(“identityNumber”,IDPU编号);
json.put(“dob”,dob);
json.put(“性别”,性别);
json.put(“emailId”,Email);
json.put(“mobileNumber”,手机号码);
json.put(“stdCode”,国家代码);
json.put(“座机号码”,座机号码);
json.put(“国家代码”,国家);
put(“stateCode”,State);
put(“zipcode”,Pincod);
json.put(“状态”,status_tc);
//put(“密码”,password);
//json.put(“地址”,address);
jsonArray.put(json);
试一试{
登录。输入(“密码”,密码);
jsonArray.put(登录);
}捕获(异常){
异常。printStackTrace();
}
试一试{
地址。放入(“地址”,地址);
jsonArray.put(地址);
}捕获(异常){
异常。printStackTrace();
}
试一试{
identity.put(“identityType”,Idproof_-typ);
identity.put(“identityNumber”,IDPU编号);
jsonArray.put(标识);
}捕获(异常){
异常。printStackTrace();
}
StringEntity se=新的StringEntity(jsonArray.toString());
setContentType(新的BasicHeader(HTTP.CONTENT_TYPE,“application/json”);
塞滕邮政
{
  UserVO: {
    userId: "",
    firstName: ""
  }
}
 public void postData(){  
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yourdomain.com/post.php");  

    try {
        // Add your data
        List nameValuePairs = new ArrayList(1);

        // Your user variable which are directly send from here 
        nameValuePairs.add(new BasicNameValuePair("data1", "dataValue"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        InputStream is = response.getEntity().getContent();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(20);

        int current = 0;

        while((current = bis.read()) != -1){
            baf.append((byte)current);
        }  

        /* Convert the Bytes read to a String. */
        text = new String(baf.toByteArray());
        txtvw.setText(text);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("url");
    List<NameValuePair> value = new ArrayList<NameValuePair>();
    value.add(new BasicNameValuePair("userid", userId)); //the userid which is inside the  "" is server variable

    //Form as like above for all variable then use below line of code to get response
    post.setEntity(new UrlEncodedFormEntity(name));

                HttpResponse response = client.execute(post);
                BufferedReader rd = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));
                String line = "";
                StringBuilder sb = new StringBuilder();
                while ((line = rd.readLine()) != null) {
                    sb.append(line + "\n");
                }
                String result = sb.toString();