(HttpConnection) Connector.open使用注意

来源:互联网 发布:sim卡和sd二合一 淘宝 编辑:程序博客网 时间:2024/05/03 08:38

 c= (HttpConnection) Connector.open(url + ";deviceside=false", Connector.READ, true);
            c.setRequestMethod(HttpConnection.GET);
            c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            c.setRequestProperty("Cache-Control", "no-store");
            c.setRequestProperty("Connection", "close"); // not sure this is a good idea, but HTTP/1.0 might be less error-prone, some clients have trouble with chunked responses
            System.out.println("debug>UTIL -- connection open;");
            
            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = c.getResponseCode();
            System.out.println("debug>UTIL -- got response code" + rc + ";");
            
            // Get the length and process the data
            int len = c.getHeaderFieldInt("Content-Length", 0);

 

----------------------------------------------

在虚拟机中测试发现,如果没有在open中的url后面加上";deviceside=false",就会出现连接超时的现象,意思就是完全不能连接获取到数据,getResponseCode()直接产生120000毫秒超时错误;

正确方式是按照 c= (HttpConnection) Connector.open(url + ";deviceside=false", Connector.READ, true);即可;

-----------------------------------------------

debug过程,包含有debug>前缀的是代码中输出的内容,其它是虚拟机发送的debug信息;

 

-----------

debug>getToken:http://api.iximo.com/request_token.php;
[0.0] debug>Consumer.java getToken:http://api.iximo.com/request_token.php;
[0.0] debug>sig mthod=HMAC-SHA1, sig class name=net.oauth.j2me.signature.HMACSHA1Signature;
[0.0] debug>Sig method=HMAC-SHA1;
[0.0] debug>not plaintext;
[0.0] debug>in signatureBaseString;
[0.0] debug>in normalizeRequestParameters;
[0.0] debug>in convertToKeyValuePairs;
[0.0] debug>done convertToKeyValuePairs;
[0.0] debug>in hastableMerge;
[0.0] debug>made it past hmerge;
[0.0] debug>made it past sort;
[0.0] debug>normalized parmas=oauth_consumer_key=7cc1a6d46dee26167629dcd5aea5f7a804d6f33d2&oauth_nonce=399[0.0] 5067585104647599&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1313027323&oauth_version=1.0;
[0.0] debug>sig base string=GET&http%3A%2F%2Fapi.iximo.com%2Frequest_token.php&oauth_consumer_key%3D7cc1a6[0.0] d46dee26167629dcd5aea5f7a804d6f33d2%26oauth_nonce%3D3995067585104647599%26oauth_signature_method%3DH[0.0] MAC-SHA1%26oauth_timestamp%3D1313027323%26oauth_version[0.0] %3D1.0;
[0.0] debug>key: 20d355a65138b4e6e7fd034dce774266%20&;
[0.0] debug>message: GET&http%3A%2F%2Fapi.iximo.com%2Frequest_token.php&oauth_consumer_key%3D7cc1a6d46dee2[0.0] 6167629dcd5aea5f7a804d6f33d2%26oauth_nonce%3D3995067585104647599%26oauth_signature_method%3DHMAC-SHA[0.0] 1%26oauth_timestamp%3D1313027323%26oauth_version%3D1.0;[0.0]
[0.0] debug>unencoded: jd?F    4?¤?‰. ?U??s? ?;
[0.0] debug>sig: amTERgk0vKTniS5/oVWh5XOGEeY=;
[0.0] debug>endpoint:http://api.iximo.com/request_token.php;
[0.0] debug>url encodedMessage :  oauth_consumer_key=7cc1a6d46dee26167629dcd5aea5f7a804d6f33d2&oauth_versi[0.0] on=1.0&oauth_signature_method=HMAC-SHA1&oauth_signature=amTERgk0vKTniS5%2FoVWh5XOGEeY%3D&oauth_times[0.0] tamp=1313027323&oauth_nonce=3995067585104647599;
[0.0] debug>visit for url = endpoint + requestMessage.convertToUrlParameters() : http://api.iximo.com/requ[0.0] est_token.php?oauth_consumer_key=7cc1a6d46dee26167629dcd5aea5f7a804d6f33d2&oauth_version=1.0&oauth_s[0.0] ignature_method=HMAC-SHA1&oauth_signature=amTERgk0vKTni[0.0] S5%2FoVWh5XOGEeY%3D&oauth_timestamp=1313027323&oauth_nonce=3995067585104647599;
[0.0] debug>UTIL open url:http://api.iximo.com/request_token.php?oauth_consumer_key=7cc1a6d46dee26167629dc[0.0] d5aea5f7a804d6f33d2&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_signature=amTERgk0vKTni[0.0] S5%2FoVWh5XOGEeY%3D&oauth_timestamp=1313027323&oauth_no[0.0] nce=3995067585104647599;
[0.0] debug>UTIL -- connection open;
[0.0] Refreshing connection on send 0
[0.0] Running refresh on GPAK DATA
[0.0] Local port: 19780
[0.0] APN: 'rim.net.gprs'
[0.0] APN username: 'null'
[0.0] APN password: 'null'
[0.0] Connection does not exist
[0.0] [ NSH ] OPEN 1
[0.0] [TMM.TunnelAllocator]:runNotReadyListeners: registered listeners found
[0.0] ** Determining Coverage **
[0.0] Last Coverage = 8
[0.0] New Coverage = 8
[0.0] New Coverage Text = 3g
[0.0] Detected information for a GPAK connection
[0.0] Refreshing connection on GPAK status
[0.0] Running refresh on GPAK GCMP
[0.0] Local port: 19780
[0.0] APN: 'rim.net.gprs'
[0.0] APN username: 'null'
[0.0] APN password: 'null'
[0.0] Connection does not exist
[0.0] Detected information for a GPAK connection
[0.0] Refreshing connection on GPAK status
[0.0] Running refresh on GPAK DATA
[0.0] Local port: 19780
[0.0] APN: 'rim.net.gprs'
[0.0] APN username: 'null'
[0.0] APN password: 'null'
[0.0] Connection exists and is open
[0.0] Detected information for a GPAK connection
[0.0] VM:PISVt=0,h=2b93,id=d15a33128dfbb5d9
[0.0] VM:ACGRt=0,c=0
[0.0] VM:GRRTr=eae7000,t=net.rim.device.api.i18n.DateFormatSymbols
[0.0] debug>UTIL -- got response code200;
[0.0] debug>content-length=1010;
[0.0] debug>Consumer RequestToken return :Invalid signature
[0.0] <hr />
[0.0] OAuthRequest Object
[0.0] (
[0.0]     [parameters:protected] => Array
[0.0]         (
[0.0]             [oauth_consumer_key] => 7cc1a6d46dee26167629dcd5aea5f7a804d6f33d2
[0.0]             [oauth_version] => 1.0
[0.0]             [oauth_signature_method] => HMAC-SHA1
[0.0]             [oauth_signature] => amTERgk0vKTniS5/oVWh5XOGEeY=
[0.0]             [oauth_timestamp] => 1313027323
[0.0]             [oauth_nonce] => 3995067585104647599
[0.0]         )
[0.0]
[0.0]     [http_method:protected] => GET
[0.0]     [http_url:protected] => http://api.iximo.com:80/request_token.php?oauth_consumer_key=7cc1a6d46de[0.0] e26167629dcd5aea5f7a804d6f33d2&oauth_version=1.0&oauth_signature_method=HMAC-SHA1&oauth_signature=am[0.0] TERgk0vKTniS5%2FoVWh5XOGEeY%3D&oauth_timestamp=13130273[0.0] 23&oauth_nonce=3995067585104647599
[0.0]     [base_string] => GET&http%3A%2F%2Fapi.iximo.com%2Frequest_token.php&oauth_consumer_key%3D7cc1a6d[0.0] 46dee26167629dcd5aea5f7a804d6f33d2%26oauth_nonce%3D3995067585104647599%26oauth_signature_method%3DHM[0.0] AC-SHA1%26oauth_timestamp%3D1313027323%26oauth_version%[0.0] 3D1.0
[0.0] )
[0.0] ;

 

---------

联网失败时,c.getResponseCode();throw出错误;

----------------


[0.0] FD Back On
[0.0] debug>Consumer.java->getRequestToken error: java.io.InterruptedIOException: Local connection timed o[0.0] ut after ~ 120000 for visit http://api.iximo.com/request_token.php?oauth_consumer_key=7cc1a6d46dee26[0.0] 167629dcd5aea5f7a804d6f33d2&oauth_version=1.0&oauth_sig[0.0] nature_method=HMAC-SHA1&oauth_signature=Cu75JdJD7Rfor9gkK7MA9sFiUB8%3D&oauth_timestamp=1313033421&oa[0.0] uth_nonce=4993284119449246057;

---------------

测试发现一个奇怪现象:debug状态时,console中的字符总是被[0.0]分节,就算是一串完整字符,并没有包含[0.0],我以为是变量中也是包含有[0.0],发现并不是;

计算一下,发现基本上是每节是101个字符;

难道是用数组来装字符,然后join时用[0.0];

这只是猜测,但这不影响debug,没资料好查暂不"追究"它;

 

---------------

补充:

有人说在虚拟机中,需要把deviceside设置true,而在真机中需要设置成false,在虚拟机中设置成false,有时也能成功;

因为我没有真机,没法测试了;

但是在虚拟机中,在设置成true后,再打开radio(虚拟手机信号),且打开wifi就可以使用了;

(HttpConnection) Connector.open使用注意 - qidizi - qidizi 的博客

原创粉丝点击