Diego Jefferies
Diego Jefferies

Reputation: 11

POST method is not set

During debug, I realize that all the request property and method I set is not executed at all. Can anyone explain why?

Because of this, whenever I request an output from URLConnection, that line will create a runtime error.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        url = new URL("https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute?api-version=2.0&format=swagger");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        urlConnection = (URLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
       // urlConnection.setRequestMethod("POST");

        String api_key = "Bearer api_key";
        urlConnection.setDoInput(true);

        urlConnection.addRequestProperty("Authorization", api_key);
        urlConnection.addRequestProperty("Content-Type", "application/json");
        urlConnection.addRequestProperty("Accept", "application/json");
         } catch (IOException e) {
        e.printStackTrace();
    }

   try {
        urlConnection.connect();
        urlConnection.getContent();
    } catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 0

Views: 70

Answers (3)

thepoosh
thepoosh

Reputation: 12587

  1. sending a network request in Activity.onCreate will crash your app with a NetworkOnMainThreadException, you should never do that

  2. I strongly suggest using other networking libraries which will take care of most of the things you're struggling with here such as OkHttp, Volley or Retrofit

Upvotes: 1

A.Edwar
A.Edwar

Reputation: 341

First of all you are doing network operations on UI thread which android OS does not allow so, you should put your code in an async task

like so :

AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            URL url = null;
            URLConnection urlConnection = null;
            try {
                url = new URL("https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute?api-version=2.0&format=swagger");
                urlConnection = (URLConnection) url.openConnection();
                urlConnection.setDoOutput(true);
                // urlConnection.setRequestMethod("POST");

                String api_key = "Bearer api_key";
                urlConnection.setDoInput(true);

                urlConnection.addRequestProperty("Authorization", api_key);
                urlConnection.addRequestProperty("Content-Type", "application/json");
                urlConnection.addRequestProperty("Accept", "application/json");
                urlConnection.connect();
                urlConnection.getContent();
            }
            catch (IOException exc) {
                exc.printStackTrace();
            }
            return null;
        }
    };

    task.execute();

also make sure you have intenet permission in the manifest

<uses-permission android:name="android.permission.INTERNET" />

also your api key "Bearer api_key" is not a valid api key

Upvotes: 1

try the following the send post request

try {
    url = new URL("https://ussouthcentral.services.azureml.net/workspaces/80b0369fe0df48308d956e2ddce52806/services/6379155db28f4c91b84e78d268a927f9/execute");

} catch (MalformedURLException e) {
    e.printStackTrace();
}
try {
    String requestBody = "api-version=2.0&format=swagger"
    urlConnection = (URLConnection) url.openConnection();
    String api_key = "Bearer api_key";

    urlConnection.addRequestProperty("Authorization", api_key);
    urlConnection.addRequestProperty("Content-Type", "application/json");
    urlConnection.addRequestProperty("Accept", "application/json");
    urlConnection.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(urlConnection.getOutputStream(),"UTF-8");
    writer.write(requestBody);
    writer.flush();
    if (urlconnection.getResponseCode() == 200) 
     {                  
            InputStream input = urlconnection.getInputStream();
            //parse the response
     }

  } catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Related Questions