从Android应用程序获取/发布数据到MVC3网站和viseverse
我将开发一个Android应用程序,我以前做过几次,但现在的诀窍是我需要访问一个数据库(而不是设备中的sqllite)。我们以前做过一个网站,它拥有我需要的所有功能。所以我的想法是使用该网站(MVC3)从数据库中获取信息,并从应用程序向数据库发送信息。从Android应用程序获取/发布数据到MVC3网站和viseverse,android,json,asp.net-mvc-3,gson,Android,Json,Asp.net Mvc 3,Gson,我将开发一个Android应用程序,我以前做过几次,但现在的诀窍是我需要访问一个数据库(而不是设备中的sqllite)。我们以前做过一个网站,它拥有我需要的所有功能。所以我的想法是使用该网站(MVC3)从数据库中获取信息,并从应用程序向数据库发送信息。 你们有什么想法告诉我怎么编码吗?我需要的是使用Json或Gson从网站接收数据到应用程序,然后我需要从应用程序向控制器发布一些数据,不知道我是否需要使用url参数,或者我是否也可以这样使用Jsom?您可以使用Json请求。ASP.NET MVC
你们有什么想法告诉我怎么编码吗?我需要的是使用Json或Gson从网站接收数据到应用程序,然后我需要从应用程序向控制器发布一些数据,不知道我是否需要使用url参数,或者我是否也可以这样使用Jsom?您可以使用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)
{
...
}
您可以向it部门发送以下POST
请求:
POST /mycontroller/myaction
Content-Length: 22
Content-Type: application/json; charset=UTF-8
Host: www.example.com
{"name":"foo","age":5}
要在ASP.NET控制器中发送json对象,请执行以下操作: {“姓名”:“福”,“年龄”: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();
}
public void GetDataFromServer(){
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“数据1”,“数据”));
添加(新的BasicNameValuePair(“数据”);
试试{
HttpPost HttpPost=新的HttpPost(“http://path_to_the_controller_on_server");
字符串结果=从HttpRequest(httppost,nameValuePairs)检索到的数据;
//解析json数据
JSONObject jObjRoot=新的JSONObject(结果);
字符串objName=jObjRoot.getString(“名称”);
字符串objName=jObjRoot.getString(“年龄”);
}捕获(JSONException e){
Log.e(标记“错误解析数据”+e.toString());
}
}
从HttpRequest(HttpPost HttpPost,ArrayList nameValuePairs)检索的私有字符串数据{
StringBuilder sb=新的StringBuilder();
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
if(nameValuePairs!=null)
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
InputStream=entity.getContent();
//将响应转换为字符串
BufferedReader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”);
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(非法状态){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
使某人返回字符串();
}
然后如何调用此方法从mvc控制器向java-Eclipse获取信息?(在代码中)看一看我的消息的编辑谢谢很多人!让它工作起来,有没有办法对结果进行编码?例如:我在值中传递ÄÖ,但结果像一些奇怪的字符……解决了它:)将编码从iso更改为utf-8。我希望我的C#ViewModel具有数组属性。如何更改Android应用程序,使其将数组解析为参数?