我正在创建一个Android应用程序,其中我有一个用户需要填写的表单。 然后我想将插入的值发送到一个asp.net页面,该页面捕获数据并将其保存到附加到该页面的数据库中,并在将值插入数据库后,数据库向android应用程序发送响应。 像1或0一样作为反馈发送 1表示成功输入数据 0表示失败。
如果可以这样做怎么办? 请帮忙,谢谢。
答案 0 :(得分:0)
这是你的Android代码
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/myexample.aspx");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("EmpID", "12345"));
//keep adding like this
nameValuePairs.add(new BasicNameValuePair("SecondVariable", "12345"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = httpResponse.getEntity();
InputStream is = entity.getContent();
//String RESPONSE FROM SERVER
String whatIgotResponse= convertStreamToString(is);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
在ASPX页面中,您可以使用
来检索数据//all the nameValuePairs your have send
string EmpID = Request.Form["EmpID"];
//now use your basic SQL Connection with the values to insert
SQL........
// after the SQL Query you can use
Response.Write("Something Something");