2012-02-22 29 views
1

我要开发一个Android应用程序,我以前做过几次,但现在的技巧是我需要访问数据库(不是设备中的sqllite)。我们之前完成了一个网站,其中包含我需要的所有功能。所以我的想法是使用该网站(MVC3)从数据库获取信息,并从App发送信息到数据库。 你们有我的一些想法如何编码?我需要的是从网站接收数据到应用程序与Json或Gson我猜,那么我需要从应用程序发布一些数据到控制器,不知道如果我需要使用网址参数,或者如果我可以使用Jsom这样呢?从Android应用程序获取/发布数据到MVC3网站和视图

回答

1

要发送在ASP.NET控制器中的json对象,如: {“name”:“foo”,“age”:5}

控制器的代码可能是这样的:

[HttpPost] 
public JsonResult MyAction(UserViewModel UserModel) 
{ 
    /* do something with your usermodel object */ 
    UserModel.Name = "foo"; 
    UserModel.Age = 5; 

    return Json(UserModel, JsonRequestBehavior.AllowGet); 
} 

编辑: 在Android方面,在这里它是发送请求和接收响应的方法:

public void GetDataFromServer() { 

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("data_1", "data")); 
    nameValuePairs.add(new BasicNameValuePair("data_n", "data")); 
    try { 
    HttpPost httppost = new HttpPost("http://path_to_the_controller_on_server"); 
    String result = RetrieveDataFromHttpRequest(httppost,nameValuePairs); 
    // parse json data 
    JSONObject jObjRoot = new JSONObject(result); 
    String objName = jObjRoot.getString("Name"); 
     String objName = jObjRoot.getString("Age"); 
    } catch (JSONException e) { 
    Log.e(TAG, "Error parsing data " + e.toString()); 
    } 
} 

private String RetrieveDataFromHttpRequest(HttpPost httppost, ArrayList<NameValuePair> nameValuePairs) { 

    StringBuilder sb = new StringBuilder(); 
    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     if (nameValuePairs != null) 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     InputStream is = entity.getContent(); 
     // convert response to string 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1")); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return sb.toString(); 
} 
+0

你如何然后调用这个方法来获取从mvc控制器到java的信息 - Eclipse? (在代码中) – 2012-06-08 08:48:20

+0

看看我的消息编辑 – superbre 2012-06-08 21:16:47

+0

谢谢你!得到它的工作,有没有一种方法来编码结果?例如:即时通过ÅÄÖ的值,但出来像一些奇怪的字符.., – 2012-06-23 16:23:42

1

您可以使用JSON请求。 ASP.NET MVC 3有一个内置的JsonValueProvider,它允许将JSON请求反序列化为强类型的视图模型。例如,让我们假设你有以下型号:

public class MyViewModel 
{ 
    public string Name { get; set; } 
    public int Age { get; set; } 
} 

和下面的控制器操作:

[HttpPost] 
public ActionResult MyAction(MyViewModel model) 
{ 
    ... 
} 

你可以在以下POST请求发送给它:

POST /mycontroller/myaction 
Content-Length: 22 
Content-Type: application/json; charset=UTF-8 
Host: www.example.com 

{"name":"foo","age":5} 
相关问题