Results 1 to 5 of 5
Thread: Problem using postmethod
- 12-01-2010, 10:25 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Problem using postmethod
Hello everyone,
for a university project we are using a webservice.
This webservice is used for communicating between an android application and our MySQL database.
Now we are in the test stage of the database DAO classes that will run on our server.
For one reason or another we are able to use getmethods to pass parameters to our server but we are not while using postmethods. The strange thing about this: some postmethods ARE working. I was wondering if there are any restrictions in using postmethods and if I did something wrong.
A few codelines from AnnouncementDAO:
A few codelines from our testclass:PHP Code:import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import org.json.simple.JSONObject; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @Path ("/AnnouncementHandler") public class AnnouncementDAO { protected DatabaseManager manager = DatabaseManager.getInstance(); /* *This method IS NOT working */ @POST @Path ("/getAnnouncementByWord") @Produces ("application/json") public String getAnnouncementByWord(@QueryParam("word") String word){ String query = " WHERE title like '%" + word + "%' or message like '%" + word + "%'"; String executeQuery = "SELECT * FROM announcement"; System.out.println("executeQuery before:" + executeQuery); System.out.println("word:"+ word); if(word !=null){ executeQuery +=query; } System.out.println("executeQuery after:" +executeQuery); String result = queryForAnnouncements(executeQuery); manager.disconnect(); return result; } private String queryForAnnouncements(String query) { JsonArray announcements = new JsonArray(); ResultSet rs = manager.query(query); Gson gson = new Gson(); try { while(rs.next()) { JsonObject announcement = (JsonObject) gson.toJsonTree(manager.getColumnValues(rs)); JsonElement jsonElement = announcement.get("courseCode"); if (!jsonElement.isJsonNull()) { String courseCode = jsonElement.getAsString(); query = "SELECT * FROM course WHERE courseCode='" + courseCode + "'"; JsonArray result = querySimpleTable(query); if(result.size() >0)announcement.add("course", result.get(0)); } announcements.add(announcement); } } catch (SQLException e) { JSONObject result = new JSONObject(); result.put("result", "SQLException : (ERR:" + e.getErrorCode() + ") " + e.getMessage()); return result.toString(); } String asString = announcements.toString(); return asString; } private JsonArray querySimpleTable(String query) { Vector announcements = new Vector(); ResultSet rs = manager.query(query); Gson gson = new Gson(); try { while(rs.next()) { announcements.add(manager.getColumnValues(rs)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return (JsonArray) gson.toJsonTree(announcements); } /* *This method IS working */ @POST @Path ("/addAnnouncement") @Produces ("application/json") public String addAnnouncement(@FormParam("message") String message, @FormParam("userId") String userId, @FormParam("title") String title, @FormParam("courseCode") String courseCode, @FormParam("date") String date){ JSONObject result = new JSONObject(); if(userId != null) { try { String query = "INSERT INTO announcement (announcementId,message,userId,title,courseCode,date) VALUES (NULL,'"+ message + "','" + userId + "','" + title + "','" + courseCode + "'," + date +")"; System.out.println(query); manager.update(query); manager.disconnect(); } catch (SQLException e) { result.put("result", "SQLException : (ERR:" + e.getErrorCode() + ") " + e.getMessage()); return result.toString(); } result.put("result", "Announcement sucessfully added"); } else { result.put("result", "The userId was empty, no User added..."); } return result.toString(); } }
The strange thing about this is that changing the method type for the getAnnouncement() to @GET (and changing the code appropriately) DOES work but we want to use it as a POST method.PHP Code:import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.io.IOUtils; import org.apache.log4j.BasicConfigurator; //import org.apache.log4j.BasicConfigurator; import be.kuleuven.cw.peno3.model.Announcement; import com.google.gson.Gson; import com.google.gson.JsonParseException; public class ClientDAOTest { public static void main(String[] args) { testAddAnnouncements(); testGetAnnouncement(); } private static void testAddAnnouncements() { try { HttpClient client = new HttpClient(); PostMethod method = new PostMethod("http://" + ipAdress.getIp() + "/AnnouncementHandler/addAnnouncement"); method.addParameter("message", "Dit is de postmethod test"); method.addParameter("userId","s0215121"); method.addParameter("title", "De eerste postmethode werkt!"); method.addParameter("courseCode", "Randomcode"); Calendar calendar = Calendar.getInstance(); calendar.set(2010, 11, 29, 22, 14); Date date = calendar.getTime(); String dateString = toMysqlDate(date); method.addParameter("date", dateString); int returnCode = client.executeMethod(method); System.out.println(method.getResponseBodyAsString()); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void testGetAnnouncement() { try { HttpClient client = new HttpClient(); PostMethod method = new PostMethod("http://" + ipAdress.getIp() + "/AnnouncementHandler/getAnnouncementByWord"); // method.setQueryString("?word=test"); method.addParameter("word","test"); int returnCode = client.executeMethod(method); String json = method.getResponseBodyAsString(); if(json.contains("[]")) { System.out.println("Geen zoekresultaten gevonden"); } else { System.out.println(json); Announcement[] obj2 = new Gson().fromJson(json.toString(), Announcement[].class); for (Announcement announcement : obj2) { System.out.println("Message = "+announcement.getMessage()); System.out.println("Title = "+announcement.getTitle()); System.out.println("UserId = "+ announcement.getUserId()); System.out.println("Coursecode = "+announcement.getCourseCode()); System.out.println("AnnouncementId = " + announcement.getAnnouncementId()); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static String toMysqlDate(Date date){ if (date==null) return "NULL"; SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sqlValueWithQuotas(sdf.format(date)); } public static String sqlValueWithQuotas(Object obj){ if ( obj == null ) return "NULL"; String str = obj.toString(); str.replaceAll("'", "\\'"); str = '\''+str+'\''; return str; } }
Thanks in advance
Bougey
- 12-01-2010, 10:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
What happens when you POST to it?
"Does not work" doesn't give us much to go on.
- 12-01-2010, 11:05 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
The parameter isn't passed to the server.
At the serverside we receive nullobjects for the passed parameter.
Thanks in advance
Bougey
- 12-01-2010, 12:18 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Edit: Nevermind, wrong thread (and it took forever to get this edit page loaded, so sorry if some other replies have already been made to this).
Last edited by masijade; 12-01-2010 at 01:03 PM.
- 12-02-2010, 07:25 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks