这是我在这段代码中的代码 try 块中的值我得到了httpconn = null的值,但是在第一行中,我将值存储到第一行的变量httpconn中,为什么会这样。我得到nullpointerexception。
公共静态字符串 requestToken() { 字符串 url = Const.REQUEST_TOKEN_URL; 字符串标头 = oauth_header(url, HttpProtocolConstants.HTTP_METHOD_GET); 字符串 requestTokenUrl = concatURL(url, header); HttpConnection httpConn = null; 输入流输入=空; 尝试 { httpConn = (HttpConnection) Connector.open(requestTokenUrl); // 克里斯连接 httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET); httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/"); httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 输入 = httpConn.openDataInputStream(); int resp = httpConn.getResponseCode(); if (resp == HttpConnection.HTTP_OK) { StringBuffer 缓冲区 = new StringBuffer(); 整数通道; 而 ( (ch = input.read()) != -1) { buffer.append((char) ch); } 字符串内容 = buffer.toString(); Const.token = content.substring(content.indexOf((Const.OAUTH_TOKEN+"="))+(Const.OAUTH_TOKEN+"=").length(), content.indexOf('&')); Const.tokenSecret = content.substring(content.indexOf((Const.OAUTH_TOKEN_SECRET+"="))+(Const.OAUTH_TOKEN_SECRET+"=").length(), content.length()); 消息 = httpConn.getResponseMessage(); } 返回消息;//(getTwitterMessage(httpConn.getResponseCode())); } 捕获(IOException e) { 返回“异常”; } 捕获(异常 nc) { 返回“无连接”; } 最后 { 尝试 { httpConn.close(); 输入.close(); } 捕捉(IOException e){ e.printStackTrace(); }