0

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: 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;

        }
Bugs
  • 4,491
  • 9
  • 32
  • 41
im07
  • 386
  • 2
  • 12

2 Answers2

0

You didn't Connect your url connection.

add

httpURLConnection.connect()  

just before the

int getPostStatus = httpURLConnection.getResponseCode();

and do the rest only if url connection response is ok like

if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  //do your stuff
}

you can check my answer here also : https://stackoverflow.com/a/36099358/2319627

UPDATE

Your code seems ok if you added the httpURLConnection.connect(). I tried your URL, but found the file is not available. Please give a specific file address (like systechdigital.com/projects/friendLocation/login/checkLogin/index.php) as the url and try.

Community
  • 1
  • 1
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
  • Please give me your new logcat – Arun Shankar Mar 20 '16 at 04:21
  • 03-20 10:54:01.541 9066-9602/com.tracker.systechdigital.realtimetrackingandmonitoring V/GetPostSts: 403 03-20 10:54:01.551 9066-9602/com.tracker.systechdigital.realtimetrackingandmonitoring V/GetPostCatchIOE: java.io.FileNotFoundException: http://systechdigital.com/projects/friendLocation/login/checkLogin – im07 Mar 20 '16 at 04:54
  • I checked your server. It's not available in php also. So try giving a specific php file address as url. have you given an index.php file in the folder checkLogin? – Arun Shankar Mar 20 '16 at 09:21
  • Dear #DevTest this a MVC app, that's why the (.php) is not available. "login" is the controller and checkLogin is the method of that controller. – im07 Mar 20 '16 at 10:04
0

After several attempt the problem is solved. I was getting that 403 response for the mentioned line below: httpURLConnection.setChunkedStreamingMode(0); i was setting the chunked length 0 forcefully that was the problem. thank you all, for your kind co-operation. For more information follow the link : http://developer.android.com/reference/java/net/HttpURLConnection.html#setChunkedStreamingMode(int)

im07
  • 386
  • 2
  • 12