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?
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:
setDoOutput(true)
if they include a request body. Transmit data by writing to the stream returned by getOutputStream()
.getInputStream()
. If the response has no body, that method returns an empty stream.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);
....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With