weixin_39492595
Java一点都不慌
2017-11-30 07:17

JSP发送短信信息,调用Java类方法

  • java
  • mobile

谢了这样一个类方法 ,我想要在JSP调用,并且执行这个类方法

package javaapplication;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;

import org.json.JSONException;
import org.json.JSONObject;
/**
*

  • @author Administrator
    */
    public class Api {
    public static void main(String[] args) {
    Api api = new Api();
    String httpResponse = api.testSend();
    try {
    JSONObject jsonObj = new JSONObject( httpResponse );
    int error_code = jsonObj.getInt("error");
    String error_msg = jsonObj.getString("msg");
    if(error_code==0){
    System.out.println("Send message success.");
    }else{
    System.out.println("Send message failed,code is "+error_code+",msg is "+error_msg);
    }
    } catch (JSONException ex) {
    Logger.getLogger(Api.class.getName()).log(Level.SEVERE, null, ex);
    }

    httpResponse =  api.testStatus();
    try {
        JSONObject jsonObj = new JSONObject( httpResponse );
        int error_code = jsonObj.getInt("error");
        if( error_code == 0 ){
            int deposit = jsonObj.getInt("deposit");
            System.out.println("Fetch deposit success :"+deposit);
        }else{
            String error_msg = jsonObj.getString("msg");
            System.out.println("Fetch deposit failed,code is "+error_code+",msg is "+error_msg);
        }
    } catch (JSONException ex) {
        Logger.getLogger(Api.class.getName()).log(Level.SEVERE, null, ex);
    }
    

    }

    private String testSend(){
    // just replace key here
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter(
    "api","key-9e434e0d08c4154762378561143a1f5c"));
    WebResource webResource = client.resource(
    "http://sms-api.luosimao.com/v1/send.json");
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    formData.add("mobile", "sMSPhone");
    formData.add("message", "验证码:逗比【铁壳测试】");
    ClientResponse response = webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
    post(ClientResponse.class, formData);
    String textEntity = response.getEntity(String.class);
    int status = response.getStatus();
    //System.out.print(textEntity);
    //System.out.print(status);
    return textEntity;
    }

    private String testStatus(){
    Client client = Client.create();
    client.addFilter(new HTTPBasicAuthFilter(
    "api","key-9e434e0d08c4154762378561143a1f5c"));
    WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
    MultivaluedMapImpl formData = new MultivaluedMapImpl();
    ClientResponse response = webResource.get( ClientResponse.class );
    String textEntity = response.getEntity(String.class);
    int status = response.getStatus();
    //System.out.print(status);
    //System.out.print(textEntity);
    return textEntity;
    }
    }

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答