2012-02-26 5 views
0

나는 user-unput을 수집하고 서버의 PHP 스크립트로 보내야하는 어플리케이션을 개발 중이다. 나는 데이터를 게시하기 위해 클래스를 사용하는 방법을 보여준 코드를 검색했다. ..하지만 데이터는 내가 그것을 누락하고있는 server.Where에 게시되지 않습니다? ... 다음은 내가 클릭하면 내 코드의 일부는 ..HTTP Post to 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

답변

0
  1. 필요 읽기, 또한

    <?php 
    // GET method 
    print_r($_GET); 
    
    // POST method 
    print_r($_POST); 
    
    ?> 
    

    당신의 시뮬레이션 된 WiFi 네트워크로 Default WLAN Network을 사용하십시오.

  2. 서버에서 HTTP 응답 코드를 수신하는지 확인하십시오. ProcessTransaction 메서드에서 문을 if 문으로 이동 한 다음 디버그하여 서버에서 HTTP_OK 응답을 받을지 확인합니다. 그렇게한다면 연결이 작동하고 PHP 스크립트 나 처리 방법과 관련이 있다는 것을 알 수 있습니다. 그렇지 않은 경우 네트워크 연결입니다.

당신이 당신의 블랙 베리에서 사용할 수있는 연결 유형을 결정하는 '무거운'할 수있는 ConnectionFactory 클래스 확인 할 수 있습니다 OS5 또는 이상을 사용하는 경우 (와이파이, 3G를 등 EDGE) 나와 URL을 만듭니다.