I am posting some data to a webservice here is the snippet:
try {
response = UniversalHttpUrlConnection.postJSONObject(ctx.getResources().getString(R.string.delete_one), UniversalHashMapToJSONObjectParams.createObjectForParams(deleteMap));
System.out.println("Response from del task" + response);
if(response != null)
JSONObject jObj = new JSONObject(response);
System.out.println("Deletion" + DatabaseHandler.getInstance(ctx).removeReceipt(receipt));
} catch (JSONException report) {
report.printStackTrace();
}
My IDE is complaining about:
if(response != null)
JSONObject jObj = new JSONObject(response);
The exact statement being: Not a statement - Why is this happening, am I missing something here?
if I use:
if (response != null) {
JSONObject jObj = new JSONObject(response);
}
The IDE does not complain.
For details here is my complete method:
public static void deleteReceipt(Receipt receipt, Context ctx) {
SessionManager sessionManager;
sessionManager = new SessionManager(ctx);
HashMap<String, String> map = sessionManager.getUserDetails();
String email = map.get("email");
if (CheckConnection.isNetworkConnected(ctx)) {
System.out.println("local id " + receipt._id);
Receipt rec = DatabaseHandler.getInstance(ctx).getReceipt(receipt._id);
String webReceiptId = rec.web_receipt_id;
System.out.println("WebReceipt ID = " + webReceiptId);
if (webReceiptId.equalsIgnoreCase("dummy")) {
// Delete from local
System.out.println("Deletion" + DatabaseHandler.getInstance(ctx).removeReceipt(receipt));
} else {
// delete from server
// delete from local
HashMap<String, String> deleteMap = new HashMap<>();
deleteMap.put("email", email);
deleteMap.put("type", "Android");
deleteMap.put("key", Utility.getencryptkey());
deleteMap.put("task_id", webReceiptId);
deleteMap.put("receipt_id", String.valueOf(receipt._id));
String response = null;
try {
response = UniversalHttpUrlConnection.postJSONObject(ctx.getResources().getString(R.string.delete_one),
UniversalHashMapToJSONObjectParams.createObjectForParams(deleteMap));
System.out.println("Response from del task" + response);
if (response != null) {
JSONObject jObj = new JSONObject(response);
}
System.out.println("Deletion" + DatabaseHandler.getInstance(ctx).removeReceipt(receipt));
} catch (JSONException report) {
report.printStackTrace();
}
}
}
}
A variable declaration such as JSONObject jObj = ...
must be inside a block.
Therefore either :
if(response != null) {
JSONObject jObj = new JSONObject(response);
}
or
JSONObject jObj = ...
if(response != null)
jObj = new JSONObject(response);
are valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With