2016-03-03 55 views
0

我已經附加了我的HTTP類現在我需要打電話給這個類,當我需要發送我的短信按鈕點擊。我似乎沒有找到任何解決辦法。任何幫助將深表感謝。調用HTTP API使用Java發送短信

public class Sender { 
    // Username that is to be used for submission 
    String username; 
    // password that is to be used along with username 
    String password; 
    // Message content that is to be transmitted 
    String message; 
    /** 
    * What type of the message that is to be sent 
    * <ul> 
    * <li>0:means plain text</li> 
    * <li>1:means flash</li> 
    * <li>2:means Unicode (Message content should be in Hex)</li> 
    * <li>6:means Unicode Flash (Message content should be in Hex)</li> 
    * </ul> 
    */ 
    String type; 
    /** 
    * Require DLR or not 
    * <ul> 
    * <li>0:means DLR is not Required</li> 
    * <li>1:means DLR is Required</li> 
    * </ul> 
    */ 
    String dlr; 
    /** 
    * Destinations to which message is to be sent For submitting more than one 
    * destination at once destinations should be comma separated Like 
    * 91999000123,91999000124 
    */ 
    String destination; 
    // Sender Id to be used for submitting the message 
    String source; 
    // To what server you need to connect to for submission 
    String server; 
    // Port that is to be used like 8080 or 8000 
    int port; 
    public Sender(String server, int port, String username, String password, 
        String message, String dlr, String type, String destination, 
        String source) { 
     this.username = username; 
     this.password = password; 
     this.message = message; 
     this.dlr = dlr; 
     this.type = type; 
     this.destination = destination; 
     this.source = source; 
     this.server = server; 
     this.port = port; 
    } 
    private void submitMessage() { 
     try { 
      // Url that will be called to submit the message 
      URL sendUrl = new URL("http://" + this.server + ":" + this.port 
        + "/bulksms/bulksms"); 
      HttpURLConnection httpConnection = (HttpURLConnection) sendUrl 
        .openConnection(); 
       // This method sets the method type to POST so that 
       // will be send as a POST request 
      httpConnection.setRequestMethod("POST"); 
      // This method is set as true wince we intend to send 
      // input to the server 
      httpConnection.setDoInput(true); 
     // This method implies that we intend to receive data from server               httpConnection.setDoOutput(true); 
// Implies do not use cached data 
      httpConnection.setUseCaches(false); 
     // Data that will be sent over the stream to the server. 
      DataOutputStream dataStreamToServer = new DataOutputStream(
        httpConnection.getOutputStream()); 
      dataStreamToServer.writeBytes("username=" 
        + URLEncoder.encode(this.username, "UTF-8") + "&password=" 
        + URLEncoder.encode(this.password, "UTF-8") + "&type=" 
        + URLEncoder.encode(this.type, "UTF-8") + "&dlr=" 
        + URLEncoder.encode(this.dlr, "UTF-8") + "&destination=" 
        + URLEncoder.encode(this.destination, "UTF-8") + "&source=" 

        + URLEncoder.encode(this.source, "UTF-8") + "&message=" 
        + URLEncoder.encode(this.message, "UTF-8")); 
      dataStreamToServer.flush(); 
      dataStreamToServer.close(); 
      // Here take the output value of the server. 
      BufferedReader dataStreamFromUrl = new BufferedReader(
        new InputStreamReader(httpConnection.getInputStream())); 
      String dataFromUrl = "", dataBuffer = ""; 
      // Writing information from the stream to the buffer 
      while ((dataBuffer = dataStreamFromUrl.readLine()) != null) { 
       dataFromUrl += dataBuffer; 
      } 
    /** 
    * Now dataFromUrl variable contains the Response received from the 
    * server so we can parse the response and process it accordingly. 
    */ 
      dataStreamFromUrl.close(); 
      System.out.println("Response: " + dataFromUrl); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
    public void main(String[] args) { 
     try { 
// Below exmaple is for sending Plain text 
      Sender s = new Sender("http:", 8080, "xxxxxxx", 
        "xxxxx", "Congratulations! You just gave someone a priceless gift - LIFE! Thank you for donating." + 
        "Your next donation date is 13/6/16. Get ", "1", "0", "xxxxxxx", 
        "xxxx"); 
      s.submitMessage(); 
     // Below exmaple is for sending unicode 
      Sender s1 = new Sender("smpp2.routesms.com", 8080, "xxxx", 
        "xxx", convertToUnicode("test for unicode").toString(), 
        "1", "2", "919869533416", "Update"); 
      s1.submitMessage(); 
     } catch (Exception ex) { 
     } 
    } 
    /** 
    * Below method converts the unicode to hex value 
    * @param regText 
    * @return 
    */ 
    private StringBuffer convertToUnicode(String regText) { 
     char[] chars = regText.toCharArray(); 
     StringBuffer hexString = new StringBuffer(); 
     for (int i = 0; i < chars.length; i++) { 
      String iniHexString = Integer.toHexString((int) chars[i]); 
      if (iniHexString.length() == 1) { 
       iniHexString = "000" + iniHexString; 
      } 
      else if (iniHexString.length() == 2) 
       iniHexString = "00" + iniHexString; 
      else if (iniHexString.length() == 3) 
       iniHexString = "0" + iniHexString; 
      hexString.append(iniHexString); 
     } 
     System.out.println(hexString); 
     return hexString; 
    } 
} 

我似乎無法從Android應用程序調用此類,通過短信發送短信APi我已附加。

當我想在按鈕單擊時發送短信時,我已將我的呼叫附加到發件人類以調用http類。 按鈕按鈕; button =(Button)findViewById(R.id.noBtn); button.setOnClickListener(新View.OnClickListener(){ @覆蓋 公共無效的onClick(查看視圖){

  //createUserAppointment(); 

      Sender sender = new Sender("xxxxxxxxxxxxx", 8080, "xxxxx", 
        "xxxx", "Congratulations! You just gave someone a priceless gift - LIFE! Thank you for donating." + 
        "Your next donation date is 13/6/16.", "1", "0", "xxxxxxxxx", 
        "Moja"); 
      sender.submitMessage(); 


     } 


    }); 

回答

0

什麼產生你的企圖?錯誤?還是它編譯良好,但一點兒也不做任何事?請分享任何輸出過

那麼你把「XXXXXXXXX」在args,因爲你不想讓我們看到真正的ADRESS還是因爲你只是複製粘貼的例子嗎?

否則我建議你看看這裏 I am trying http connection over android

+0

我設法解決您的解決方案,但這次我得到無效的用戶名和密碼值。相同的用戶名和密碼是我用來登錄發送我的批量短信。我有這個想法。 – jonathan

+0

我也設法解決這個問題。我爲呼叫使用了錯誤的服務器名稱。從API提供商那裏獲得了正確的服務器名稱。 – jonathan