BlackBerry HttpCOnnection



/*嗨,我正在开发一个应用程序,其中BB应用程序需要将数据发布到服务器。Http连接在黑莓模拟器上运行良好,但当我试图在真实设备上测试它时,应用程序无法将数据发送到服务器。以下是我的代码:*/

package com.sims.datahandler;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import com.sims.commonmethods.CommonMethods;
import com.sims.screens.MenuScreen;
/**
 * 
 * @author SaiKrishnaPawar
 *
 */
public class GPRSHandler extends Thread {
        private String data;
        private String url;
        private String msgKey;
        private String mobileNumber;
        public String sendGPRSRequest() {
             HttpConnection httpConn = null;
             DataOutputStream oStrm = null;
             DataInputStream is = null;
             byte[] resp = null;
             String responseData;
             try {
                 // Creating httpconnection object to handle GPRS request
                 httpConn = (HttpConnection) Connector.open(url);
                 httpConn.setRequestMethod(HttpConnection.POST);
                 httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Confirguration/CLDC-1.0");
                 httpConn.setRequestProperty("Accept_Language", "en-US");
                 httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                 oStrm = httpConn.openDataOutputStream();
                 byte dataArray[] = (mobileNumber + "&" + msgKey + data).getBytes();
//               byte dataArray[] = (msgKey + data).getBytes();
                 CommonMethods.getSystemOutput("msg key and data:::"+mobileNumber + msgKey + data);
                 for (int i = 0; i < dataArray.length; i++) {
                     oStrm.writeByte(dataArray[i]);
                 }
                 DataInputStream din = httpConn.openDataInputStream();
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 int ch;
                 while ((ch = din.read()) != -1) {
                     baos.write(ch);
                 }
                 resp = baos.toByteArray();
                 responseData = new String(resp);
                 baos.close();
                 din.close();
                 httpConn.close();
                 return responseData.trim();
             } catch (IOException ex) {
                 CommonMethods.getSystemOutput("IO Exception in run method of gprs handler::" + ex.getMessage());
                 UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                    int choice = Dialog.ask(Dialog.D_OK, "No Connectivity");
                    exitApp(choice);
                    }
                });
             } catch (NullPointerException nex) {
                 CommonMethods.getSystemOutput("NullPointerException:" + nex.getMessage());
             } catch (SecurityException e) {
                 CommonMethods.getSystemOutput("SecurityException:" + e.getMessage());
                 UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        Dialog.ask(Dialog.OK, "Security Exception");
                        UiApplication.getUiApplication().pushScreen(new MenuScreen());
                    }
                });
             } finally {
                 try {
                     if (is != null) {
                         is.close();
                     }
                     if (oStrm != null) {
                         oStrm.close();
                     }
                     if (httpConn != null) {
                         httpConn.close();
                     }
                 } catch (Exception ex) {
                     UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            Dialog.ask(Dialog.OK, "ERROR in While Connecting GPRS Connection");
                            UiApplication.getUiApplication().pushScreen(new MenuScreen());
                        }
                    });
                 }
             }
             return null;
        }
        public void setData(String data) {
            this.data = data;
        }
        public void setMsgKey(String msgKey) {
            this.msgKey = msgKey;
        }
        public void setUrl(String url) {
            this.url = url + ";deviceside=false";
        }
        public void setMobileNumber(String mobileNumber) {
            this.mobileNumber = mobileNumber;
        }
        private void exitApp(int choice) {
            System.exit(0);
        }
}
   httpConn = (HttpConnection) Connector.open(url);

你可以这样写://

   url = url + ";deviceside=false";
   httpConn = (HttpConnection) Connector.open(url);

请在这一行添加网络扩展

 httpConn = (HttpConnection) Connector.open(url);
url末尾的

请检查您是否添加了url扩展名假设你使用的是wifi那么你必须添加

   httpConn = (HttpConnection) Connector.open(url+";interface=wifi");

这是为接口工作,如果你想其他类型的网络,请参考我的答案在这里

"隧道Failed"BlackBerry Curve 8520的例外

相关内容

  • 没有找到相关文章

最新更新