-4

I use post man to test the url (post), it looks fine. API:http://www.dmapp.com.tw/MobileApp/GetHealthData.php

But when i try it on my android , i can't get the message even my response code is 200

My logcat shows {"Msg":"Request method not accepted","JsonData":null,"ErrorCode":"0099"}

I do not get it , i used the code to get the json data well before.

Some one can teach me what step i miss it , that would be appreciated.

Here is my get json data function:

private String getRoute(String url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        int responseCode = connection.getResponseCode();
        StringBuilder jsonIn = new StringBuilder();
        // responseCode show 200
        Log.d("responseCode:",responseCode+"");
        if (responseCode == 200) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                jsonIn.append(line);
            }
        } else {
            Log.d(TAG, responseCode + "responseCode");
        }
        connection.disconnect();
        // I can't get the json data it shows Request method not accepted
        Log.d(TAG, jsonIn + "jsonIn");
        return jsonIn.toString();

    }
Morton
  • 5,380
  • 18
  • 63
  • 118

3 Answers3

2

The HttpURLConnection is sending a GET request by default.
Your link is working perfectly.
Try adding
connection.setRequestMethod("POST");

Reference

Sam
  • 58
  • 7
1

The response from server itself is null as JsonData, You should check at server side.

GreenROBO
  • 4,725
  • 4
  • 23
  • 43
1

Hello your Link is working its just that its a Post Request

private String getRoute(String url) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        int responseCode = connection.getResponseCode();
        connection.setRequestMethod("POST");


        StringBuilder jsonIn = new StringBuilder();
        // responseCode show 200
        Log.d("responseCode:",responseCode+"");
        if (responseCode == 200) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                jsonIn.append(line);
            }
        } else {
            Log.d(TAG, responseCode + "responseCode");
        }
        connection.disconnect();
        // I can't get the json data it shows Request method not accepted
        Log.d(TAG, jsonIn + "jsonIn");
        return jsonIn.toString();

    }

I hope it will work But In My opinion you should work with Volley or Retrofit instead of HttpURLConnection

Avinash
  • 264
  • 5
  • 15