给出java.lang.IllegalStateException的setRequestProperty方法
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"); 嗨,我得到java.lang.IllegalStateException:调用setRequestProperty方法后,无法设置连接后的方法错误,但是当我在连接之前调用此方法时,我得到NullPointerException,因为con为null.我该怎么做才能解决这个问题? 解决方法
请从Google:
http://developer.android.com/reference/java/net/HttpURLConnection.html查看此网址
在设置标题之前可以使用openConnection()方法.以下是文档中的步骤: >通过调用URL.openConnection()并将结果转换为HttpURLConnection来获取新的HttpURLConnection. 但是,如果问题仍然存在,您可以尝试调试代码并检查connection.connected私有标志以查看它变为真的位置;调用getContentType()方法后,我遇到了类似的问题. 否则你可以切换到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); .... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |