5
HttpURLConnection con = null;
        Response response = new Response();
        String TAG = "HttpConHandler";

        try{
            /*
             * IMPORTANT: 
             * User SHOULD provide URL Encoded Parms
             */
            Log.p(TAG, "URL="+ urlStr);
            String q=httpHeaders.get("Authorization");

            URL url = new URL(urlStr);
            con = (HttpURLConnection) url.openConnection(); 

            con.setRequestProperty("Authorization", q);
            con.setRequestProperty("GData-Version", "3.0");

Hi I am getting java.lang.IllegalStateException: Cannot set method after connection is made error when setRequestProperty method is called ,but when I call this method before connection I get NullPointerException because con is null. What can I do to solve this?

user207421
  • 305,947
  • 44
  • 307
  • 483
Harinder
  • 11,776
  • 16
  • 70
  • 126
  • seems pretty clear.open the connection after you set your properties – Ovidiu Latcu Oct 11 '11 at 12:50
  • @Ovidiu Latcu but i get a nullpointer exception cause con is not initialized – Harinder Oct 11 '11 at 12:51
  • The code you have posted does not behave as described. You aren't calling `setResponseCode()`, which is a servlet method, not an `HttpURLConnection` method, and you aren't changing the method of the connection either, as suggested by the exception message. Clearly the real problem is that you are calling `setRequestProperty()` or `setRequestMethod()` or `getOutputStream()` in some other manner. Please post the real code. – user207421 Jun 23 '15 at 10:08
  • It's the debugger: see http://stackoverflow.com/questions/22320418/setrequestproperty-throwing-java-lang-illegalstateexception-cannot-set-request[enter link description here](http://stackoverflow.com/questions/22320418/setrequestproperty-throwing-java-lang-illegalstateexception-cannot-set-request) – sdgian Apr 21 '16 at 13:59

3 Answers3

1

Please check this URL from Google : http://developer.android.com/reference/java/net/HttpURLConnection.html

It is ok to use the openConnection() method before setting your headers. Here are the steps from the documentation:

  1. Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
  2. Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.
  3. Optionally upload a request body. Instances must be configured with setDoOutput(true) if they include a request body. Transmit data by writing to the stream returned by getOutputStream().
  4. Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.
  5. Disconnect. Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.

However if the problem persists you can either try to debug you code and check the connection.connected private flag to see where it becomes true; I've experienced a similar problem after making a call to the getContentType() method.

Otherwise you can just switch to HttpClient API:

  HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url); 

    // Execute the request
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
....
Bogdan Veliscu
  • 641
  • 6
  • 11
0

Don't use URL's convenience method, either use an HttpURLConnection or other library.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

This may work:

URL url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection(); 
con.setDoOutput(true);
con.setRequestProperty("Authorization", q);
con.setRequestProperty("GData-Version", "3.0");
Matt MacLean
  • 19,410
  • 7
  • 50
  • 53
  • I am not sure if setting doOutput requires you to also open an OutputStream and actually write data, would require some playing around. Maybe checkout this post too: http://stackoverflow.com/questions/2026260/java-how-to-use-urlconnection-to-post-request-with-authorization/2026299#2026299 – Matt MacLean Oct 11 '11 at 13:04
  • didnt work still the same exception now for con.setDoOutput(true); – Harinder Oct 12 '11 at 04:43
  • Looks like on Android you cannot do this.. See this post: http://stackoverflow.com/questions/5979087/how-can-i-use-http-auth-information-from-a-url-in-android – Matt MacLean Oct 12 '11 at 05:52