Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

android - setRequestProperty method giving java.lang.IllegalStateException: Cannot set method after connection is made

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
....

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...