Commit c3d07c6c authored by Stephen's avatar Stephen
Browse files

Fixed sendfind

parents 5b30e317 f90f2004
compile.on.save=true
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
\ No newline at end of file
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
......@@ -188,12 +188,11 @@ public class TCPClient {
return obj;
}
public JSONObject findOfferJSON(String token, int id_item) {
public JSONObject findOfferJSON(int id_item) {
JSONObject obj = new JSONObject();
try {
obj.put("method", "findoffer");
obj.put("token", token);
obj.put("item", id_item);
} catch(Exception e) {
e.printStackTrace();
......@@ -601,11 +600,11 @@ public class TCPClient {
return responseJSON;
}
public JSONObject findoffer(String token, int id_item) throws Exception
public JSONObject findoffer(int id_item) throws Exception
{
openConnection();
System.out.println("Find Offer");
String toSend = findOfferJSON(token, id_item).toString();
String toSend = findOfferJSON(id_item).toString();
//throw ioexception
DataOutputStream outToServer;
......@@ -800,7 +799,10 @@ public class TCPClient {
response = client.offer(client.token, 0, 1, 7, 3);
System.out.println(response.toString());
response = client.sendFind(client.token, 2);
// response = client.sendFind(client.token, 2);
// System.out.println(response.toString());
response = client.findoffer(6);
System.out.println(response.toString());
}
......
compile.on.save=true
user.properties.file=C:\\Users\\Stephen\\AppData\\Roaming\\NetBeans\\8.0.2\\build.properties
user.properties.file=C:\\Users\\user\\AppData\\Roaming\\NetBeans\\8.0\\build.properties
......@@ -406,20 +406,20 @@ public class MySQLAccess {
+ "offered_item=" + id_item + " AND available = 1;");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
response.put("status", "ok");
JSONArray array = new JSONArray();
array.put(resultSet.getInt("offered_item"));
array.put(resultSet.getInt("n1"));
array.put(resultSet.getInt("demanded_item"));
array.put(resultSet.getInt("n2"));
array.put(resultSet.getBoolean("available"));
array.put(resultSet.getString("offer_token"));
response.put("offers", array);
}
else
{
response.put("status", "error");
while(resultSet.next()) {
int user_id = resultSet.getInt("id_user");
if(user_id != 0)
{
response.put("status", "ok");
JSONArray array = new JSONArray();
array.put(resultSet.getInt("offered_item"));
array.put(resultSet.getInt("n1"));
array.put(resultSet.getInt("demanded_item"));
array.put(resultSet.getInt("n2"));
array.put(resultSet.getBoolean("available"));
array.put(resultSet.getString("offer_token"));
response.put("offers", array);
}
}
} catch (SQLException e) {
response.put("status", "error");
......@@ -430,7 +430,7 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
public JSONObject sendAccept(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
......@@ -523,6 +523,43 @@ public class MySQLAccess {
return response;
}
public JSONObject accept(int userID, String offer_token) throws JSONException
{
open();
JSONObject response = new JSONObject();
try {
//cek offer dengan offer token yang sesuai
preparedStatement = connect.prepareStatement("SELECT * FROM offer WHERE "
+ "offer_token=\"" + offer_token + "\";");
resultSet = preparedStatement.executeQuery();
if(resultSet.first()) {
boolean available = resultSet.getBoolean("available");
//cek apakah offer tersedia
if(available)
{
preparedStatement = connect.prepareStatement("UPDATE offer SET available = 0" + " WHERE "
+ "offer_token=\"" + offer_token + "\";");
preparedStatement.executeUpdate();
response.put("status", "ok");
}
else //offer tidak tersedia
{
response.put("status", "fail");
response.put("description", "offer is not available");
}
}
}
catch (SQLException e) {
response.put("status", "error");
e.printStackTrace();
}
close();
return response;
}
public JSONObject fetchitem(int userID, String offer_token) throws JSONException
{
open();
......
......@@ -385,6 +385,21 @@ public class TCPServer implements Runnable {
}
}
public boolean isJSONValid(String test) {
try {
new JSONObject(test);
} catch (JSONException ex) {
// edited, to include @Arthur's comment
// e.g. in case JSONArray is valid as well...
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false;
}
}
return true;
}
public static void main(String[] args) throws Exception {
TCPServer server = new TCPServer();
......@@ -408,7 +423,7 @@ public class TCPServer implements Runnable {
System.out.println("Received: " + server.clientRequest + "aaaaaaa");
if(!server.clientRequest.equalsIgnoreCase("")) {
if(!server.clientRequest.equalsIgnoreCase("") && server.isJSONValid(server.clientRequest)) {
// Mementuk JSON Object dari request client
JSONObject clientObject = new JSONObject(server.clientRequest);
String method = clientObject.getString("method");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment