如何在jersey webservice中接受json数组并将其发送到数据库

时间:2016-10-21 21:56:07

标签: android json web-services rest jersey

您好我是泽西岛的新人,我想将数据发布到webservice然后发布到数据库。当我发送一个json对象时,一切都很完美,当我尝试发布json数组时会出现问题。 这是我的代码:

Android App

httpURLConnection.connect();
JSONArray jsonArray = new JSONArray();
JSONObject contener = new JSONObject();
for (int i=0;i<2;i++){
    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "object" + i);
        jsonArray.put(jsonObject);

    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
contener.put("list", jsonArray);
OutputStream os = httpURLConnection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write(contener.toString());
osw.flush();
osw.close();
Log.i("TAG", "close");
Log.i("Response Code", "" + httpURLConnection.getResponseCode());

web服务:

@POST
@Path("/activity")
public Response postMsg(RecognizedActivityList list)  {
    SessionFactory sessionFactory = DBUtil.getSessionFactory();

    Session session = sessionFactory.openSession();
    for (RecognizedActivity activity: list.getList()) {
        try {
            test activityTable = new test(activity.getType());

            session.beginTransaction();
            session.save(activityTable);
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(e.getMessage());
        } finally {
            session.close();
        }
    }
    return Response.status(Response.Status.OK).build();
}

RecognizedActivity

public class RecognizedActivity {

    public String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

RecognizedActivityList

public class RecognizedActivityList {

    private List<RecognizedActivity> list;

    public List<RecognizedActivity> getList() {
        return list;
    }

    public void setList(List<RecognizedActivity> list) {
        this.list = list;
    }
}

测试实体

@Entity
public class test implements Serializable {
    private static final long serialVersionUID = -6823112948469660293L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long id;
        @Column
        private String type;

        public test (){}
        public test(String type){
            this.type=type;}
        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }
    }
}

响应代码为500。

有人能帮帮我吗?我会感激一段代码:)

修改 问题解决了。答案如下。包装器是列表的声明(类似于RecognizedActivityList)

 @Path("/activity")
    public Response postMsg(Wrapper dataList) {
        SessionFactory sessionFactory = DBUtil.getSessionFactory();

        Session session = sessionFactory.openSession();
        try {
            session.beginTransaction();
            if (dataList.getActivities() != null) {
                for (RecognizedActivity activity : dataList.getActivities()) {
                    ActivityTable activityRecord = new ActivityTable(activity.activityType, new Date(activity.getDataStart()), activity.getTotalTime());
                    session.save(activityRecord);
                }
            }

            session.getTransaction().commit();

        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(e.getMessage());
        } finally {
            session.close();
        }
        //String jObject = "" + activity.getActivityType();
        return Response.status(Response.Status.OK).build();
    }

0 个答案:

没有答案