2012-02-26 55 views
0

我正在开发一个应用程序,我需要收集用户输入并发送到服务器上的PHP脚本。我搜索了一些代码,告诉我如何使用类发布数据。 ..但该数据不会发布到server.Where我失去了它?..下面是我的一些代码..HTTP发布到PHP

import net.rim.device.api.ui.*; 
    import net.rim.device.api.ui.component.*; 
    import net.rim.device.api.ui.container.*; 
    import net.rim.device.api.system.*; 

    /** 
    * 
    */ 
    class IntroPage extends MainScreen{ 
// private members - these represent the "controls" 
    private BasicEditField identifierfield = null; 
    private BasicEditField datafield = null; 
     private ButtonField submitbutton = null; 
    private LabelField statusfield = null; 
    IntroPage() { 
    super(); 
    setTitle("BB IBM Demo App"); 
    identifierfield = new BasicEditField("Identifier: ","",50, EditField.NO_NEWLINE);  
    // add this field to the screen 
    add(identifierfield); 

    // create a field for the data of our transaction 
    datafield = new BasicEditField("Data: ","",100, EditField.NO_NEWLINE);  
    // add this field to the screen 
    add(datafield); 
    // createlistener 
    FieldChangeListener listener = new FieldChangeListener() { 
      public void fieldChanged(Field field, int context) { 
       ButtonField submitbutton = (ButtonField) field; 
       String id=identifierfield.getText(); 
       String data=datafield.getText(); 
       if(id.trim().length()==0||data.trim().length()==0) 
       { 
       Dialog.alert("Please Fill In All Fields."); 
       return; } 
       if(bb_ibm_transaction.ProcessTransaction(id,data)==true) 
       { 
        Dialog.alert("Your Data Has Been Saved Successfully"); } 
       else 
       { 
       Dialog.alert("There Was An Error Saving Your Details,Please Try Again Later."); } 
} 
     }; 

    // create a button to submit our transaction 
    submitbutton = new ButtonField("Submit Transaction",ButtonField.CONSUME_CLICK); 
    submitbutton.setChangeListener(listener); 


    // add this button to the screen 
    add(submitbutton); 

    // add a status label 
    statusfield = new LabelField("Please enter transaction data."); 

当我点击提交button..i得到“有是保存您的详细资料的错误“对话...(这意味着连接类别不会回应'成功'...从而返回false) 下面是Connection类的代码:

import net.rim.blackberry.api.browser.URLEncodedPostData; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import javax.microedition.io.HttpConnection; 
    import javax.microedition.io.Connector; 
    /** 
    * 
    */ 
    class bb_ibm_transaction { 
    bb_ibm_transaction() { } 
// this method interacts with the server 
public static boolean ProcessTransaction(String id,String data) 
{ 
    // default to non-success return code 
    boolean ret = false; 

    // some variables necessary for HTTP communication 
    InputStream inputStream = null; 
    HttpConnection httpConnection = null; 


    // because many of the steps can throw an exception, wrap this method in   try/catch block 
    try 
    { 

     StringBuffer returnStringBuffer = new StringBuffer(); 
     String returnString = new String(); 


     String desiredEncoding = "ISO-8859-1"; 


     URLEncodedPostData params = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, true); 
     params.append("identifier", id); 
     params.append("data", data); 

     String url = "http://localhost/mob/test.php?" + params.toString(); 

     System.out.println(url); 


     //Connecting to Server 
     httpConnection = (HttpConnection)Connector.open(url); 
     inputStream = httpConnection.openDataInputStream(); 

     if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK) 
     { 
      int ch; 

      //Process Response 

      // check header field for a specific encoding 
      String contenttype = httpConnection.getHeaderField("Content-Type"); 
      if (contenttype != null) 
      { 
       contenttype = contenttype.toUpperCase(); 
       if (contenttype.indexOf("UTF-8") != -1) 
       { 
        desiredEncoding = "UTF-8"; 
       } 
      } 

      // get an inputstreamreader to handle utf-8 data 
      InputStreamReader isr = new InputStreamReader(inputStream,desiredEncoding); 

      while ((ch = isr.read()) != -1) 
      { 
       returnStringBuffer.append((char) ch); 
      } 

      inputStream.close(); 
      httpConnection.close(); 
      inputStream = null; 
      httpConnection = null; 
      returnString = returnStringBuffer.toString(); 

      // examine return string 
      if (returnString.indexOf("SUCCESS") != -1) 
      { 
       ret = true; 
      } 
      return ret; 
     } 
     inputStream.close(); 
     httpConnection.close(); 
     inputStream = null; 
     httpConnection = null; 
     //Bad Transaction. 
     return ret; 
    } 
    catch (Exception e) 
    { 
     System.out.println("Error occurred in ProcessTransaction(" + id + "," + data + ")\n" + e.toString()); 
     return ret; 
    } 
    finally 
    { 
     try 
     { 
      if (inputStream != null) 
       inputStream.close(); 
      if (httpConnection != null) 
       httpConnection.close(); 
     } 
     catch (Exception ee) 
     { 
     } 
    } 


} 
    } 

这是PHP脚本

<?php 
    include 'mob_connect.php'; 
    $id=mysqli_real_escape_string($link,$_POST['id']); 
if($id) 
{ 
echo "SUCCESS"; 
} 
?> 

如果我失去了它?

+0

服务器日志中有趣的事情? – miki 2012-02-26 09:41:13

+0

您是在真实设备上还是在模拟器上测试? – donturner 2012-02-27 09:26:02

回答

0
  1. 如果在模拟器上测试添加;interface=wifi到您的网址并使用Default WLAN Network作为您的模拟WiFi网络。

  2. 检查您是否从服务器获取HTTP响应代码。在ProcessTransaction方法中,移动if语句中的inputStream = httpConnection.openDataInputStream();行,然后调试以查看是否从服务器获得HTTP_OK响应。如果你这样做,你知道连接正在工作,这与你的PHP脚本或你处理它的方式有关。如果没有,这是你的网络连接。

如果您使用的OS5或以上,你可能要检查出ConnectionFactory类可以做确定哪些类型的连接都可以到BlackBerry的“繁重”(WIFI,3G,EDGE等)并为您创建网址。