When I try to make a HTTP POST over Android it's showing the following exception:
java.io.FileNotFoundException
However the same thing is working fine whilst browsing from a browser on the desktop.
It is showing the exception when executing this line:
InputStream inputStream = httpURLConnection.getInputStream();
This is my logcat:
03-19 17:47:16.003 24673-24739/com.tracker.systechdigital.realtimetrackingandmonitoring I/OpenGLRenderer: Get enable program binary service property (1)
03-19 17:47:39.510 24673-25359/com.tracker.systechdigital.realtimetrackingandmonitoring V/GetPostA: http://systechdigital.com/projects/friendLocation/login/checkLogin
03-19 17:47:40.056 24673-25359/com.tracker.systechdigital.realtimetrackingandmonitoring V/GetPostSts: 403
03-19 17:47:40.060 24673-25359/com.tracker.systechdigital.realtimetrackingandmonitoring W/System.err: at com.tracker.systechdigital.realtimetrackingandmonitoring.GetPostAsyncTask.doInBackground(GetPostAsyncTask.java:108)
03-19 17:47:40.060 24673-25359/com.tracker.systechdigital.realtimetrackingandmonitoring W/System.err: at com.tracker.systechdigital.realtimetrackingandmonitoring.GetPostAsyncTask.doInBackground(GetPostAsyncTask.java:28)
03-19 17:47:40.060 24673-25359/com.tracker.systechdigital.realtimetrackingandmonitoring V/GetPostCatchIOE: java.io.FileNotFoundException: http://systechdigital.com/projects/friendLocation/login/checkLogin
I have tried with the below code:
@Override
protected String doInBackground(String... args) {
try {
// setting the URL
URL url = new URL(baseUrl+args[1]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.addRequestProperty("User-Agent", "RealTimeApps/1.0");
// setting the method type
httpURLConnection.setRequestMethod(args[0]);
httpURLConnection.setChunkedStreamingMode(0);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
Log.v("Url",args[2]);
// setting the identification key name with query params
bufferedWriter.write(args[2]);
bufferedWriter.flush();
bufferedWriter.close();
Log.v("GetPostA", url.toString());
int getPostStatus = httpURLConnection.getResponseCode();
Log.v("GetPostSts", String.valueOf(getPostStatus));
String line = "";
String res = "";
// if(getPostStatus == 200){
// prepare the output buffer
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
while ((line = bufferedReader.readLine()) != null) {
res += line;
}
inputStream.close();
// }
httpURLConnection.disconnect();
switch (args[3]){
case "1":
responseStrLogin = res.toString();
break;
case "2":
responseStrSignUp = res.toString();
break;
case "3":
responseStrFriendList = res.toString();
break;
case "4":
responseStrUpdateLocation = res.toString();
break;
case "5":
responseStrSettings = res.toString();
break;
case "6":
responseStrSearchFriendForRequest = res.toString();
break;
case "7":
responseStrSendFriendRequest = res.toString();
break;
case "8":
responseStrUserFriendRequestList = res.toString();
break;
case "9":
responseStrApproveFriendRequest = res.toString();
break;
case "10":
responseStrProfileDetails = res.toString();
break;
case "11":
responseStrCarDetails = res.toString();
break;
case "12":
responseStrCorporateDetails=res.toString();
break;
case "13":
responseStrPasswordChange=res.toString();
break;
case "14":
responseStrLocationShareToggle=res.toString();
break;
case "15":
responseStrToggleStatus=res.toString();
break;
case "16":
responseStrForgetPassword=res.toString();
break;
case "17":
responseStrSearchResult=res.toString();
break;
case "18":
responseStrDeleteFriend=res.toString();
break;
case "19":
responseStrSearchResultFriendList=res.toString();
break;
case "20":
responseStrShowFriendList=res.toString();
break;
case "21":
responseStrShowFriendProfileDetails=res.toString();
break;
case "22":
responseStrTimeSetForIndividuals=res.toString();
break;
case "23":
responseStrFriendListPeriod=res.toString();
break;
case "24":
responseStrFriendsRouteLatLng=res.toString();
break;
default:
responseStr = res.toString();
break;
}
Log.v("ResD", res.toString());
return res.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
Log.v("GetPostCatchMal",e.toString());
} catch (IOException e) {
e.printStackTrace();
Log.v("GetPostCatchIOE", e.toString());
}
return null;
}