在BlackBerry中单击按钮时获取Web响应(Getting web response when button clicked in BlackBerry)
我在BB中加载了一个网页,如下所示
//RegBrowserFieldConfig extends BrowserFieldConfig RegBrowserFieldConfig regBrowserFieldConfig = new RegBrowserFieldConfig(); //RegBrowserFieldListener extends BrowserFieldListener RegBrowserFieldListener regBrowserFieldListener = new RegBrowserFieldListener(); BrowserField registrationBrowserField = new BrowserField(regBrowserFieldConfig); registrationBrowserField.addListener(regBrowserFieldListener); add(registrationBrowserField); registrationBrowserField.requestContent("http://myurl.com/");该网页加载正常。 该网页中有一个提交按钮,它在HTML中的表单元素中调用onsubmit 。 那就是调用JavaScript函数。 在该函数中,还有一些其他URL将根据要求触发。
我需要的是获得这些URL调用的响应。 我怎样才能做到这一点?
I have loaded a web page in BB as follow
//RegBrowserFieldConfig extends BrowserFieldConfig RegBrowserFieldConfig regBrowserFieldConfig = new RegBrowserFieldConfig(); //RegBrowserFieldListener extends BrowserFieldListener RegBrowserFieldListener regBrowserFieldListener = new RegBrowserFieldListener(); BrowserField registrationBrowserField = new BrowserField(regBrowserFieldConfig); registrationBrowserField.addListener(regBrowserFieldListener); add(registrationBrowserField); registrationBrowserField.requestContent("http://myurl.com/");That web page loads fine. There is a submit button in that web page which call onsubmit in the form element in HTML. That is calling to a JavaScript function. With in that function there are some other URL that will fire according to the requirements.
What I need is to get the response of those URL calls. How can I do that?
最满意答案
我试过这种方式..
BrowserFieldListener list = new BrowserFieldListener() { public void documentLoaded(BrowserField browserField, Document document) throws Exception { String url = document.getBaseURI(); //u can get the current url here... u can use ur logic to get the url after clicking the submit button Serverconnection(url);//from this methode u can get the response } }; browserField.addListener(list);一个ServerConnection ..
public String Serverconnection(String url) { String line = ""; // if (DeviceInfo.isSimulator()) { // url = url + ";deviceSide=true"; // } else { // url = url + ";deviceSide=true"; // } url = url + getConnectionString(); try { HttpConnection s = (HttpConnection) Connector.open(url); s.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); s.setRequestProperty( "Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); s.setRequestProperty(HttpProtocolConstants.HEADER_ACCEPT_CHARSET, "UTF-8"); s.setRequestMethod(HttpConnection.GET); InputStream input = s.openInputStream(); byte[] data = new byte[10240]; int len = 0; StringBuffer raw = new StringBuffer(); while (-1 != (len = input.read(data))) { raw.append(new String(data, 0, len)); } line = raw.toString(); input.close(); s.close(); } catch (Exception e) { System.out.println("response--- excep" + line + e.getMessage()); } return line; }编辑..
private static String getConnectionString() { String connectionString = ""; if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { connectionString = "?;interface=wifi"; } else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { connectionString = "?;&deviceside=false"; } else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { String carrierUid = getCarrierBIBSUid(); if (carrierUid == null) { connectionString = "?;deviceside=true"; } else { connectionString = "?;deviceside=false?;connectionUID=" + carrierUid + "?;ConnectionType=mds-public"; } } else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) { } return connectionString; } private static String getCarrierBIBSUid() { ServiceRecord[] records = ServiceBook.getSB().getRecords(); int currentRecord; for (currentRecord = 0; currentRecord < records.length; currentRecord++) { if (records[currentRecord].getCid().toLowerCase().equals("ippp")) { if (records[currentRecord].getName().toLowerCase() .indexOf("bibs") >= 0) { return records[currentRecord].getUid(); } } } return null; }I tried this way..
BrowserFieldListener list = new BrowserFieldListener() { public void documentLoaded(BrowserField browserField, Document document) throws Exception { String url = document.getBaseURI(); //u can get the current url here... u can use ur logic to get the url after clicking the submit button Serverconnection(url);//from this methode u can get the response } }; browserField.addListener(list);Serverconnection..
public String Serverconnection(String url) { String line = ""; // if (DeviceInfo.isSimulator()) { // url = url + ";deviceSide=true"; // } else { // url = url + ";deviceSide=true"; // } url = url + getConnectionString(); try { HttpConnection s = (HttpConnection) Connector.open(url); s.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); s.setRequestProperty( "Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); s.setRequestProperty(HttpProtocolConstants.HEADER_ACCEPT_CHARSET, "UTF-8"); s.setRequestMethod(HttpConnection.GET); InputStream input = s.openInputStream(); byte[] data = new byte[10240]; int len = 0; StringBuffer raw = new StringBuffer(); while (-1 != (len = input.read(data))) { raw.append(new String(data, 0, len)); } line = raw.toString(); input.close(); s.close(); } catch (Exception e) { System.out.println("response--- excep" + line + e.getMessage()); } return line; }EDIT..
private static String getConnectionString() { String connectionString = ""; if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) { connectionString = "?;interface=wifi"; } else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) { connectionString = "?;&deviceside=false"; } else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) { String carrierUid = getCarrierBIBSUid(); if (carrierUid == null) { connectionString = "?;deviceside=true"; } else { connectionString = "?;deviceside=false?;connectionUID=" + carrierUid + "?;ConnectionType=mds-public"; } } else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) { } return connectionString; } private static String getCarrierBIBSUid() { ServiceRecord[] records = ServiceBook.getSB().getRecords(); int currentRecord; for (currentRecord = 0; currentRecord < records.length; currentRecord++) { if (records[currentRecord].getCid().toLowerCase().equals("ippp")) { if (records[currentRecord].getName().toLowerCase() .indexOf("bibs") >= 0) { return records[currentRecord].getUid(); } } } return null; }更多推荐
发布评论