如何在使用HttpPost请求之前将数据添加到JSONObject?

时间:2016-08-18 12:28:59

标签: java android arrays json

我有一个类似下面的json数据::

{

     "lastName":"sadfsdf",
       "email":"acb@bbc.com",
       "gender":"male",
       "workids":[0,0],
       "roleId":102
}

对于workids参数我使用了params的ArrayList。

{
List<Integer> ll = new ArrayList<Integer>();
ll.add(100);
ll.add(102);
}

我将params添加到像这样的json对象

{

    JSONObject json = new JSONObject();
    json.put("lastName", "bbdddb");
    json.put("email","abc@bbc.com");
    json.put("gender","male");
    json.put("secGameIds",ll);
    json.put("roleId",secGame);
}

这是我提出的要求。

{

     HttpPost post = new HttpPost(uri);
     post.setHeader("Content-type", "application/json");
     post.setEntity(new StringEntity(json.toString(), "UTF-8"));
     DefaultHttpClient client = new DefaultHttpClient();
     HttpResponse httpresponse = client.execute(post);
    }

当我尝试运行此程序{json.put("secGameIds",ll);}时,由于此参数json.toString()以字符串而不是整数列表发送。 如何在发出请求时将该参数设置为整​​数列表?

谢谢&amp;问候。

2 个答案:

答案 0 :(得分:0)

使用jsonschema2pojo.org服务生成java类,可以使用GSON库将其解析为JSON。 在JSON中,所有数据都以String形式发送,而不是Integer,它在收到时被解析。 如您所见,下面的POJO列表是Integer列表,即使JSON有字符串。 Gson将字符串解析为整数。 你的课程看起来像(这是一个很好的做法):

-----------------------------------com.example.User.java-----------------------------------

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class User {

@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("email")
@Expose
private String email;
@SerializedName("gender")
@Expose
private String gender;
@SerializedName("workids")
@Expose
private List<Integer> workids = new ArrayList<Integer>();
@SerializedName("roleId")
@Expose
private Integer roleId;

/**
* 
* @return
* The lastName
*/
public String getLastName() {
return lastName;
}

/**
* 
* @param lastName
* The lastName
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

/**
* 
* @return
* The email
*/
public String getEmail() {
return email;
}

/**
* 
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}

/**
* 
* @return
* The gender
*/
public String getGender() {
return gender;
}

/**
* 
* @param gender
* The gender
*/
public void setGender(String gender) {
this.gender = gender;
}

/**
* 
* @return
* The workids
*/
public List<Integer> getWorkids() {
return workids;
}

/**
* 
* @param workids
* The workids
*/
public void setWorkids(List<Integer> workids) {
this.workids = workids;
}

/**
* 
* @return
* The roleId
*/
public Integer getRoleId() {
return roleId;
}

/**
* 
* @param roleId
* The roleId
*/
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}

}

答案 1 :(得分:0)

如果您尝试以下内容怎么办?

private void executeNetworkOperation(String jsonData) throws UnsupportedEncodingException {

    urlConnection = null;

    try {
        URL connectionUrl = new URL(url);
        urlConnection = (HttpURLConnection) connectionUrl.openConnection();
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.setConnectTimeout(timeoutConnection);
        urlConnection.setReadTimeout(timeoutRead);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Accept", "application/json");
        urlConnection.setRequestProperty("Content-type", "application/json");

        OutputStream outputStream = urlConnection.getOutputStream();
        outputStream.write(jsonData.getBytes());
        outputStream.flush();
        outputStream.close();


        InputStream is = urlConnection.getInputStream();

    } catch (MalformedURLException e){
        AppUtils.showLog(TAG, e.getMessage(), 2);
    } catch (IOException e) {
        AppUtils.showLog(TAG, e.getMessage(), 2);
    } catch (Exception e) {
        AppUtils.showLog(TAG, e.getMessage(), 2);
    }finally {
        if(null != urlConnection) {
            urlConnection.disconnect();
        }
    }
}