setRequestProperty方法的给java.lang.IllegalStateException:不能设置方法是由连接后 [英] setRequestProperty method giving java.lang.IllegalStateException: Cannot set method after connection is made

查看:2604
本文介绍了setRequestProperty方法的给java.lang.IllegalStateException:不能设置方法是由连接后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 为空。我能做些什么来解决这个?

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?

推荐答案

这可能工作:

URL url = new URL(urlStr);
con = (HttpURLConnection) url.openConnection(); 
con.setDoOutput(true);
con.setRequestProperty("Authorization", q);
con.setRequestProperty("GData-Version", "3.0");

这篇关于setRequestProperty方法的给java.lang.IllegalStateException:不能设置方法是由连接后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆