Android开发日记(四)

在服务器端数据库新建一个表ad

在DataInfo.edxm模型中点击从数据库更新模型,发布。

就新建了一个实体ad

然后新建cs文件

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using Newtonsoft.Json;
 7 using Newtonsoft.Json.Linq;
 8 
 9 namespace WebJSON.Controllers
10 {
11     public class AdShowController:Controller
12     {
13         DataInfo storedb = new DataInfo();
14         public JArray Index(int adid)
15         {
16             IEnumerable<ad> ad;
17             ad = storedb.ad.Where(u => u.adid == adid);
18             string adjson = JsonConvert.SerializeObject(ad);
19             JArray adJArray = JArray.Parse(adjson);
20 
21             return adJArray;
22 
23         }
24     }
25 }
View Code

然后发布就可以在网页端看到传递的JSON数组对象。

http://59.78.93.208:9092/AdShow?adid=1

在Android端编写service文件

 1 public class AdshowService {
 2     public JSONArray getAdshowDetails(String adid)
 3     {
 4         String url="http://59.78.93.208:9092/AdShow?adid="+adid+"";
 5         HttpClient client = new DefaultHttpClient();
 6         client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
 7         client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 3000 );
 8         HttpPost request;
 9         try {
10             request = new HttpPost(new URI(url));
11             HttpResponse response = client.execute(request);
12             if (response.getStatusLine().getStatusCode() == 200) {
13                 HttpEntity entity = response.getEntity();
14                 if (entity != null) {
15                     String out = EntityUtils.toString(entity);
16                     JSONArray jsonArray=new JSONArray(out);
17                     return jsonArray;
18                 }
19             }
20         } catch (Exception e) {
21             e.printStackTrace();
22             return null;
23         }
24         return null;
25     }
26 
27 }
View Code

 

posted @ 2015-12-05 14:14  Wei_java  阅读(204)  评论(0编辑  收藏  举报