接口调用类-FenGKun

/**
 * 接口调用类
 *
 * @author FenGKun
 *
 */
public class DataUtil {
    private static final String TAG = "DataUtil.java";
    private static final boolean isDebug = true;

    /* ****** 课程[Begin] ****** */
    /** 课程:字 */
    public static final int CATE_WORD = 1;
    /** 课程:词 */
    public static final int CATE_WORDS = 2;
    /** 课程:句 */
    public static final int CATE_SENTENCE = 3;
    /** 课程:篇 */
    public static final int CATE_PIECE = 4;
    /* ****** 课程[End] ****** */

    /**
     * 调接口
     * @param sSuffix 后缀(问号后面的内容)
     * @return 接口返回的内容
     */
    public static String CallInterface(String sSuffix) {
        String sResult = PostUrl(<span style="font-family: Arial, Helvetica, sans-serif;">域名</span><span style="font-family: Arial, Helvetica, sans-serif;"> + "Api.aspx?" + sSuffix);</span>
        if (isDebug) Log.i(TAG, "调接口:" + Const.Namespace + "Api.aspx?" + sSuffix + "\n返回:" + sResult);
        return sResult;
    }

    /**
     * 获取参数指定的网页代码,将其返回给调用者,由调用者对其解析
     * 返回String
     */
    public static String PostUrl(String url)
    {
        InputStream is = null;
        String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch (Exception e) {
            e.printStackTrace();
            return "Fail to establish http connection!" + e.toString();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        }
        catch (Exception e) {
            e.printStackTrace();
            return "Fail to convert net stream!";
        }

        return result;
    }

    /**
     * 上传文件至服务器的方法
     * @return true-发送成功,false-发送失败。
     */
    public static String uploadFile(File fFile)
    {
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        StringBuffer sbInput=null;
        try
        {
            URL urls = new URL(域名 + "uploadtx.aspx");
            HttpURLConnection con = (HttpURLConnection) urls.openConnection();
            // 允许Input、Output,不使用Cache
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);
            // 设置传送的method=POST
            con.setRequestMethod("POST");
            // setRequestProperty
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("Charset", "UTF-8");
            con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            // 设置DataOutputStream
            DataOutputStream ds = new DataOutputStream(con.getOutputStream());
//            ds.writeBytes(twoHyphens + boundary + end);
//            ds.writeBytes("Content-Disposition: form-data; " + "name=\"file1\";filename=\"" + fFile.getName() + "\"" + end);
//            ds.writeBytes(end);
            // 取得文件的FileInputStream
            FileInputStream fStream = new FileInputStream(fFile.getAbsolutePath());
            // 设置每次写入1024bytes
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int length = -1;
            // 从文件读取数据至缓冲区
            while ((length = fStream.read(buffer)) != -1)
            {
                // 将资料写入DataOutputStream中
                ds.write(buffer, 0, length);
            }
//            ds.writeBytes(end);
//            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
            // close streams
            fStream.close();
            ds.flush();
            // 取得Response内容
            InputStream is = con.getInputStream();
            int ch;
            sbInput = new StringBuffer();
            while ((ch = is.read()) != -1)
            {
                sbInput.append((char) ch);
            }
            Log.e(TAG, "服务器返回:" + sbInput.toString());
            // 关闭DataOutputStream
            ds.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "failed";
        }

        return sbInput.toString();
    }

}

</pre> <pre name="code" class="java">    /**
     * 传接口信息
     */
    private void callInterface() {
        new Thread() {
            public void run() {
<span style="white-space:pre">		</span>// 根据不同的接口字段需求传入相对应的值
                String sInfo = DataUtil.CallInterface("menu=yd&tel="+phoneNumber+"&rs="+peopleNumber+"&tp="+repastType);
                //statusJudge(sInfo);

                int iHaoMa = 0;   // 排位号码
                int iDwrs = 0;    // 等位人数

                // 解析出字列表、字评分
                try {
                    JSONObject json = new JSONObject(sInfo);
<span style="white-space:pre">			</span>// json 是返回的对象,成功为 'success',失败为'failed'
                    if (json.getString("status").equals("success")) {
<span style="white-space:pre">			</span>// 获取对象信息
                        json = new JSONObject(json.getString("message"));

                        iHaoMa = json.getInt("HaoMa");
                        iDwrs = json.getInt("Dwrs");
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(MSG_NO_NETWORD);
                    return;
                }
<span style="white-space:pre">	</span>}

}

<pre name="code" class="java">

时间: 2024-08-02 12:54:44

接口调用类-FenGKun的相关文章

接口调用类3

需要获取token的接口调用import java.io.IOException;import java.io.UnsupportedEncodingException; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.

bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能

xmlrpc .  https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很少,针对xmlrpc的有但是基本都是http协议的,https下的认证处理比较麻烦,而且会话保持也是基本没有太多共享,所以本人决定结合xmlrpc\bugzilla官方文档,网友文章,结合个人经验总结而成,已经在window2007 64+jdk7位机器上调试通过 手把手教你如何实现: 第一步: 在

基于Axis1.4的webservice接口开发(接口调用)

一.webservice接口代码参考上一篇博客: http://www.cnblogs.com/zhukunqiang/p/7125668.html 二.代码部分 1.新建Web Project工程,取名为axiswebserviceClint,并导入Axis1.4所需jar包(将jar包粘贴到lib目录). 2.新建webservice接口调用类: package com.no8.clint; import java.net.URL; import javax.xml.rpc.ServiceE

从头认识java-12.6 接口与类型信息(怎么绕过接口直接调用类的所有方法)

这一章节我们来讨论一下接口与类型信息. 在之前的章节里面我们提到接口,父类引用子类对象,然后把方法给缩窄了,但是我们这一章节学习到反射,这个约束就变得没有那么严格. 我们来看看下面的例子: package com.ray.ch11; public class Test { public static void main(String[] args) { Person man = new Man(); man.sleep(); man.run(); // man.say();//error } }

通过接口调用接口实现类实现松耦合(接口的多态)

项目开发在各个层中通过接口实现松耦合的时候,如何实现接口调用接口实现类? 这里的要求是接口有多个实现类,通过接口调用不的接口实现类! 开始,我想到了IOC(IOC最近忒火),确实有很多开发好的类库unity,ninject等等,但是这些类库都有点太强大了,例 如unity 是可以很容易实现我的要求,那么通过RegisterType<I, N>>(String name)注册:通过Resolve<I>(name) 就可以 用接口调用相应的接口实现类,如果想了解一下unity,

Spring-Boot ? ShapeFile文件读写工具类+接口调用

一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path":"c:/test", "geom":"MULTIPOLYGON(((101.870371 25.19228,101.873633 25.188183,101.880564 25.184416,101.886808 25.186028,101.89204

java后台调用HttpURLConnection类模拟浏览器请求(一般用于接口调用)

项目开发中难免遇到外部接口的调用,小生今天初次接触该类,跟着API方法走了一遍,如有不对的地方,还请哆哆指正,拜谢! 1 package com.cplatform.movie.back.test; 2 3 import java.io.BufferedReader; 4 import java.io.DataOutputStream; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 impor

ThinkPHP整合支付宝即时到账接口调用

首先是在支付宝的蚂蚁金服开放平台下载PHP的demo: 1 https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1 下载地址如上,里面有个MD5加密方式UTF8的PHP的demo可以拿来用,可以直接在localhost上跑,传入合适的参数即使体验支付一次,这点和微信支付接口调用的demo相同. 现在我们尝试将其中demo的lib类文件整合到thinkphp之中去. 将四个lib改名放入Vend

短信接口调用——阿里大于API开发心得

互联网上有许多公司提供短信接口服务,诸如网易云信.阿里大于等等.我在自己项目里需要使用到短信服务起到通知作用,实际开发周期三天,完成配置.开发和使用,总的说,阿里大于提供的接口易于开发,非常的方便,短信费用是计数缴纳的,作为个人开发者,我使用的服务产生的费用为0.45¥/条(10万条以下). 现在要实现一个例会短信群发通知的功能,所有被通知对象信息均存于Mysql中,应用架构采用asp.net MVC .首先准备好获取的API各项(以下各项服务参数都需要在大于官网上申请), 申请好自己的短信签名