Reputation: 573
I have a class with a static function to make a POST request to a web server. So far it was working for every request I've made, however I added a new .php file and it throws me a FileNotFound exception at the function call for the new script. I tested it through browser and Postman and the script works.
The class/function:
public class WebServices {
public static final String CONNECTION_URL = "http://192.168.1.101/klikniobrok/";
public static final String LOGIN = "login.php";
public static final String REGISTER = "register.php";
public static final String RESTAURANTS = "restaurant.php";
public static final String MENU = "menu.php";
public static final String MENU_ENTRIES = "menuentries.php";
public static final String EMPLOYEE_LOGIN = "employeelogin.php";
public static final String GET_TABLE = "table.php";
public static final String ADD_ORDER = "insertorder.php";
public static final String RETRIEVE_ACTIVE_ORDERS = "activeorders.php";
public static final String COMPLETE_ORDER = "completeorder.php";
public static JSONObject makeHttpPostRequest(List<Pair<String, String>> params, String action) {
JSONObject requestResponse;
try {
StringBuilder postData = new StringBuilder();
if(params != null) {
for (int i = 0; i < params.size(); i++) {
if (postData.length() != 0) {
postData.append("&");
}
postData.append(URLEncoder.encode(params.get(i).first, "UTF-8"));
postData.append("=");
postData.append(URLEncoder.encode(params.get(i).second, "UTF-8"));
}
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
URL connUrl = new URL(CONNECTION_URL + action);
HttpURLConnection connection = (HttpURLConnection) connUrl.openConnection();
connection.setRequestMethod(HttpMethods.POST);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
connection.setDoOutput(true);
connection.setDoInput(true);
if(postDataBytes.length > 0) {
connection.getOutputStream().write(postDataBytes);
}
BufferedReader requestInputReader = new BufferedReader(new InputStreamReader(connection.getInputStream(),
"UTF-8"));
StringBuilder requestInput = new StringBuilder();
for(String line = requestInputReader.readLine(); line != null; line = requestInputReader.readLine()) {
requestInput.append(line).append("\n");
}
requestInputReader.close();
connection.disconnect();
requestResponse = new JSONObject(requestInput.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
requestResponse = null;
} catch (IOException e) {
e.printStackTrace();
requestResponse = null;
} catch (JSONException e) {
e.printStackTrace();
requestResponse = null;
}
return requestResponse;
}
And the exception:
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: java.io.FileNotFoundException: http://192.168.1.101/klikniobrok/completeorder.php
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:258)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at mk.klikniobrok.klikniobrok.WebServices.makeHttpPostRequest(WebServices.java:59)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at mk.klikniobrok.klikniobrok.helpers.CompleteOrder.doInBackground(OrderProcessor.java:37)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at mk.klikniobrok.klikniobrok.helpers.CompleteOrder.doInBackground(OrderProcessor.java:31)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
06-10 00:09:52.606 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
06-10 00:09:52.607 12561-13302/mk.klikniobrok.klikniobrok W/System.err: at java.lang.Thread.run(Thread.java:818)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at mk.klikniobrok.klikniobrok.helpers.OrderProcessor.setOrderCompeted(OrderProcessor.java:164)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at mk.klikniobrok.klikniobrok.ActiveOrdersActivity$2.onClick(ActiveOrdersActivity.java:103)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at android.view.View.performClick(View.java:5226)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at android.view.View$PerformClick.run(View.java:21266)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at android.os.Looper.loop(Looper.java:168)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5781)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at java.lang.reflect.Method.invoke(Native Method)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
06-10 00:09:52.608 12561-12561/mk.klikniobrok.klikniobrok W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
The follow-up null pointer exception is a result to the first one.
The call to the function:
class CompleteOrder extends AsyncTask<Pair<String, String>, Void, JSONObject> {
@Override
protected JSONObject doInBackground(Pair<String, String>... params) {
return WebServices.makeHttpPostRequest(Arrays.asList(params), WebServices.COMPLETE_ORDER);
}
@Override
protected void onPostExecute(JSONObject response) {
// other logic here
}
}
As I stated, the function works for other 'actions', i.e when used for posting to other scripts. For example I am able to log-in just before I click the button that invokes the method. Why is this happening?
Upvotes: 0
Views: 72
Reputation: 719446
The FileNotFoundException
occurred because the server gave you a 404 Not Found
response.
You need to look at the URL you used and/or the "error page" document returned by the server and/or the server-side logs to figure out why the server did that.
As I stated, the function works for other 'actions', i.e when used for posting to other scripts
Obviously ... something is different.
Upvotes: 1