2012-02-22 110 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} 
相關問題