接口调用类-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">


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值