android项目之连接数据库(eclipse(java)+soap协议+webservice服务(c#)+sql server数据库)

 

Android客户端开发

3.1  服务器开发

服务器的主要功能是为客户端提供服务,客户端发送服务请求给服务器,服务器完成客户端的请求,然后把结果返回给客户端,因此,构建服务器的主体是构建能够满足各种服务请求的功能函数,以下列出了客户端将会用到一些函数:

☞搜索函数: public string searchButton(string keyword)

☞用户注册函数: public string UserRegister(string type, string androidId, string username, string pwd)

☞获得分类下的数据函数:public string GetContentByTypeTagTwo(string type, string TypeTag)

☞由标题到正文的映射:public string GetContentBytitle(string title)

☞用户注册函数:public string UserRegister(string type, string androidId, string username, string pwd)

☞登入日志函数: private static void VisitLog(string username, string title, string userIP ,string logType)

 

☞读图片文件并转化为字符串:

                       

string imagepath = AppDomain.CurrentDomain.BaseDirectory + @"\images\" + "localFile1.jpg";

                        FileStream fs = new FileStream(imagepath, FileMode.Open);//打开本地后保存

                        byte[] byData = new byte[fs.Length];

                        fs.Read(byData, 0, byData.Length);//现在二进制图片在bydata数组当中

                        fs.Close();

                        //有byte转化为string类型放在strResult当中

                        for (int i = 0; i < byData.Length; i++)

                        {

                            if (i == 0)

                                strResult += byData[i].ToString();

                            else

                                strResult += "." + byData[i].ToString();

                        }

 

运行服务器后web页面如下所示:

                 

     

3.2         用户客户端开发

用户客户端的主要工作是设计友好的交互界面。以下介绍客户端的主要功能:

 

任务栏分为五大类,分别是首页、热门、分类、搜索和菜单,首页、热门和分类都是文章列表,首页显示的是最新的咨询,热门显示的是一定时期内浏览量最高的文章,搜索功能可以全站搜索满足关键字的文章,分类分为四大类,分别是日常生活、全球新闻、学习天地合工作招聘,其文章列表按时间排序显示全部文章。以下是所搜界面:

 

 

菜单功能分为更新、注册、登录和退出,如下所示:

 

更新功能能够获取当前最新的咨询,点击更新以后,会同时更新首页、热门和分类下的列表文章,然后跳转到首页。

注册功能能够为用户提供注册,注册页面如下所示:

 

 

客户端界面的主要功能大体就这些。

3.3         服务器和客户端联合

服务器和用户客户端都完成以后,就要实现两者的联合,也就是客户端从服务器获取数据。客户端从服务器获取的过程如下:

①    客户端向服务器发送服务请求;

②    服务器接收客户端的请求,然后调用相关的函数,获得数据结果;

③    服务器端将数据结果处理成JSON串,然后发送给客户端;

④    客户端接收服务器的发送来的JSON串,然后保存在本地缓存中;

⑤    客户端解析JSON串,显示数据。

服务器构建JSON串代码如下:

  string json = "";

            json = "{\"type\":[{\"CrawlTitle\":\"CrawlTitle\",\"CrawlContent\":\"CrawlContent\",\"CreateTime\":\"CreateTime\",\"FirstImgUrl\":\"FirstImgUrl\"}";

            string jtemp = string.Empty;

            string one = string.Empty;

            string two = string.Empty;

            string third = string.Empty;

            string four = string.Empty;

            foreach (DataRow dataRow in dataTable.Rows)

            {

                jtemp = "\"CrawlTitle\":\"{0}\",\"CrawlContent\":\"{1}\",\"CreateTime\":\"{2}\",\"FirstImgUrl\":\"{3}\"";

                //获得该图片   网址为third = dataRow.ItemArray[3].ToString();

               

                string strResult = string.Empty;

                if (dataRow.ItemArray[3] != null && dataRow.ItemArray[3].ToString() != string.Empty && dataRow.ItemArray[3].ToString() != "")

                {

                    int localVariable=Download(dataRow.ItemArray[3].ToString(), AppDomain.CurrentDomain.BaseDirectory + @"\images\" + "localFile.jpg");//存到本地

                    if (localVariable == 1)

                    {

                        //对图片进行压缩

                        System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.Quality;//获取品质(压缩率)编码

                        EncoderParameter mycoder = new EncoderParameter(encoder, 10L);//0压缩率最大,100品质最高

                        EncoderParameters myCoders = new EncoderParameters(1);//参数数组,大小为1

                        myCoders.Param[0] = mycoder;//添加一个参数

                        ImageCodecInfo jpgInfo = GetEncoder(ImageFormat.Jpeg);//获取JPG格式编解码信息

                        Image bmp = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\images\" + "localFile.jpg");

                        bmp.Save(AppDomain.CurrentDomain.BaseDirectory + @"\images\" + "localFile1.jpg", jpgInfo, myCoders);//以指定品质率的JPG格式保存

                        bmp.Dispose();

                        //再次读取

                        string imagepath = AppDomain.CurrentDomain.BaseDirectory + @"\images\" + "localFile1.jpg";

                        FileStream fs = new FileStream(imagepath, FileMode.Open);//打开本地后保存

                        byte[] byData = new byte[fs.Length];

                        fs.Read(byData, 0, byData.Length);//现在二进制图片在bydata数组当中

                        fs.Close();

                        //有byte转化为string类型   放在  strResult当中

                        for (int i = 0; i < byData.Length; i++)

                        {

                            if (i == 0)

                                strResult += byData[i].ToString();

                            else

                                strResult += "." + byData[i].ToString();

                        }

                    }

                    else

                    {

                        strResult = "0";

                    }

                }

                else

                {

                    strResult = "0";

                }

                //转化成json串

                one = dataRow.ItemArray[0].ToString();

                two = dataRow.ItemArray[1].ToString();

                two = two.Replace("\"", "`");

                third = dataRow.ItemArray[2].ToString();

                jtemp = string.Format(jtemp, one, two, third,strResult);//暂时直接使用图片的地址发过去

                json += ",{" + jtemp + "}";

            }

            json += "]}";

            return json;

 

  客户端调用服务器函数如下:

if (!file2.exists()) {
                file2.createNewFile();
            }
            FileInputStream inputStream = openFileInput(two);
            //
            // 判断本地文件是否为空, 满足条件就条用webservice,否则取本地数据
            //
            if (inputStream.available() == 0)// 网络传则flag=0
            {
                result2 = Getwebserver.GetContentById1(third, four);
                OutputStream outStream_secd = this.getBaseContext()
                        .openFileOutput(two, Context.MODE_PRIVATE);
                content = result2.toString();// 取得的webservice数据
                fileservice.save(outStream_secd, content);
            } else {// 本地传则flag=1
                content = fileservice.read(inputStream);
                Toast.makeText(getApplicationContext(), "提示文件读取成功",
                        Toast.LENGTH_LONG).show();
            }

上面调用的 Getwebserver.GetContentById1(third, four);//函数原型如下

    private static final String NAMESPACE = "http://wswap.daxuequn.com/";
    private static String URL = "http://wswap.daxuequn.com/Service1.asmx";

    // 由类1.1. 获得9条相关的记录 获取列表
    private static final String METHO = "GetContentByTypeTagTwo";
    private static String ACTION5 = "http://wswap.daxuequn.com/GetContentByTypeTagTwo";
  
    public static String GetContentById1(String type, String TypeTag)
            throws IOException, XmlPullParserException {
        SoapPrimitive detail = null;
        try {
            String str = null;
            SoapObject rpc = new SoapObject(NAMESPACE, METHO);
//关联到NAMESPACE, METHO  进而调用服务器上面GetContentByTypeTagTwo函数进而获得json串
            rpc.addProperty("type", type);
            rpc.addProperty("TypeTag", TypeTag);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.bodyOut = rpc;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(rpc);
            envelope.encodingStyle = "UTF-8";
            HttpTransportSE ht = new HttpTransportSE(URL);
            ht.debug = true;
            ht.call(ACTION5, envelope);
            detail = (SoapPrimitive) envelope.getResponse();
            if (detail != null)
                str = detail.toString();
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

 

 客户端解析JSON串的代码如下:

  

JSONObject jsonResponse;

       try {

           jsonResponse = new JSONObject(jsonStr);

           // 转换成jsonobject对象

           JSONArray jsonArray = jsonResponse.getJSONArray("type");// 获取type属性的value

                                                            // 为一个数组

           String title = null;

           String info = null;

           for (int i = 1; i < jsonArray.length(); i++) {

              title = jsonArray.getJSONObject(i).getString("CrawlTitle");

              // info=jsonArray.getJSONObject(i).getString("CrawlContent");

              Map<String, Object> map = new HashMap<String, Object>();

              map.put("title", title);

 

              String inputstream = jsonArray.getJSONObject(i).getString(

                     "FirstImgUrl");// 解析出图片流 字符串

              String dtr = null;

              if (inputstream.equals("0") || inputstream.equals("")) {

                  map.put("img", R.drawable.up);

              } else {

                  String[] byte_stream = inputstream.split("\\.");

                  int num = byte_stream.length;

                  byte[] bs = new byte[num];

                  for (int j = 0; j < num; j++) {

                     int a = Integer.parseInt(byte_stream[j]);

                     char c = (char) a;

                     byte bbb = (byte) c;

                     bs[j] = bbb;

                  }

                  /* 解析好的bitmap */

                  Bitmap newbm = ImageDispose.zoomBitmap(

                         ImageDispose.BytestoBitmap(bs), 72, 72);

                  map.put("img", newbm);

              }

              list.add(map);

           }

       } catch (JSONException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       } catch (Exception e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       return list;

 

 

文件的缓存实现机制保证无网络仍然可以看新闻,缓存的函数如下:

 

if (inputStream.available() == 0)//网络传则flag=0

 {           

 result2 =Getwebserver.GetContentById1(third,four);

 OutputStream outStream_secd = this.getBaseContext().openFileOutput(two, Context.MODE_PRIVATE);

 content = result2.toString();//取得的webservice数据 

 fileservice.save(outStream_secd, content);  

 }    

 else {//本地传则flag=1

 content = fileservice.read(inputStream);

 Toast.makeText(getApplicationContext(), "提示文件读取成功", Toast.LENGTH_LONG).show();      

 flag =1; }  

 

获取字符串并转化为图片:

String[] byte_stream = inputstream.split("\\.");

 int num = byte_stream.length;

 byte[] bs = new byte[num];

 for(int j = 0 ;j < num ; j++)

 {

      int a =Integer.parseInt(byte_stream[j]);

      char c = (char)a;

      byte bbb = (byte) c;

      bs[j] = bbb;                 

 }

 Bitmap newbm = ImageDispose.zoomBitmap(ImageDispose.BytestoBitmap(bs) ,72,72);

 

 

解析成功后,获得的文章列表和文章内容如下所示:

    

 

使用关键字“苹果”搜索后界面如下:

 

 

注册账户,登陆后界面如下:

具体文件见百度网盘的“创新”文件夹

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值