在我的应用程序中,我想将一些客户数据从Android应用程序发送到asp.net网站(c#),所以请帮助我。
答案 0 :(得分:3)
您需要在c#端使用HTTP服务器并通过GET或POST方法或TCP服务器发送数据并通过套接字发送数据。 使用HTTP与c#,Web服务更容易。您可以使用
发送或获取数据public HttpResponse doGet() throws ClientProtocolException, IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("url with or without parameters");
HttpResponse respose = httpClient.execute(httpGet);
return respose;
}
或发布
public HttpResponse doPost() throws IOException, JSONException, ClientProtocolException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("url with or without parameters");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
return response;
}
答案 1 :(得分:2)
在您的网站中公开JSON端点。例如,如果您正在使用MVC:
public ActionResult JSONDoSomething(string param)
{
return Json("your result here", JsonRequestBehavior.AllowGet);
}
然后在客户端上使用它:
String url = "http://yourhost/yourapp/JSONDoSomething?param=" + param;
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
BasicResponseHandler handler = new BasicResponseHandler();
String result = "Nothing";
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 2 :(得分:1)
我认为你应该试试这个Tutorial。而不是ASP.NET的PHP。逻辑是相同的
它的步骤很少。
A)Android代码
ArrayList<NameValuePair> parms = new ArrayList<NameValuePair>();
parms.add(new BasicNameValuePair("EmpID", EmpID));
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
InputStream is = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.xxxx/some.aspx");
httppost.setEntity(new UrlEncodedFormEntity(parms));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
B)ASP.NET
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=RnD;Persist Security Info=True;)) //change as needed
{
using (StreamReader sr = new StreamReader(Request.InputStream, Encoding.UTF8))
{
Response.ContentType = "text/plain";
string UserID = Request.Form["EmpID"];
//SQL CODE
string c = "sql Code";
try
{
SqlCommand cmd = new SqlCommand(c, cn);
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
while (rdr.Read())
{
Dictionary<string, object> d = new Dictionary<string, object>(rdr.FieldCount);
for (int i = 0; i < rdr.FieldCount; i++)
{
d[rdr.GetName(i)] = rdr.GetValue(i);
}
list.Add(d);
}
JavaScriptSerializer j = new JavaScriptSerializer();
Response.Write("{\"JSON\":");
Response.Write(j.Serialize(list.ToArray()));
Response.Write("}");
}
catch (Exception ex)
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 500;
Response.Write("Error occurred. Query=" + c + "\n");
Response.Write(ex.ToString());
}
Response.End();
}
}
}