2012-02-26 22 views
0

私はユーザーの入力を収集し、サーバー上のPHPスクリプトに送信する必要があるアプリケーションを開発しています。私は検索し、データをポスティングするためにクラスを使用する方法を示すコードをいくつか手に入れました。 ..butデータは、私はそれをしないのです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

+0

実際のデバイスまたはシミュレータでテストしていますか? – donturner

答えて

1

アプリケーションでHTTP POSTメソッドを使用する必要があります。

PHPのすべての入力を表示してみてください:

また
<?php 
// GET method 
print_r($_GET); 

// POST method 
print_r($_POST); 

?> 

、シミュレータ上のテストは、あなたに;interface=wifiを追加する場合は、この

// Set the request method to POST 
hc.setRequestMethod(HttpConnection.POST); 
0
  1. を必要とし、このhttp://www.wirelessdevnet.com/channels/java/features/j2me_http.phtml

    を読みますあなたの擬似WiFiネットワークとしてDefault WLAN Networkを使用してください。

  2. サーバーからHTTP応答コードを取得しているかどうかを確認してください。 ProcessTransactionメソッドで行をifステートメント内に移動し、デバッグしてサーバーからHTTP_OKという応答を受け取るかどうかを確認します。もしあなたがそうしていれば、接続が機能していることを知っています。これはPHPスクリプトや処理方法と関係があります。そうでない場合、それはあなたのネットワーク接続です。

あなたはOS5を使用するか、あなたのブラックベリー(無線LAN、3G、EDGEなど)に利用可能な接続の種類を判別する「力仕事」を行うことができますConnectionFactoryクラスをチェックアウトする場合があります上記されている場合あなたのためのURLを作成します。