我的 android 应用程序需要将一些数据更新到服务器,我已经为此编写了一些 WebApi 代码来更新和从我的 android 应用程序发送数据。当我在本地服务器上测试时,两者都工作正常,但在上传到全局后它不起作用并给出错误,如:(在 android 应用程序和提琴手中测试)
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Content-Type: text/html
Server: Microsoft-IIS/8.0
我在 Android 和 C# 中都使用了简单的代码:
安卓代码:
HttpClient client = new DefaultHttpClient();
HttpPut post = new HttpPut(PUT_SETTINGS_DATA);
try {
JSONStringer vm = new JSONStringer().object().key("UserId")
.value(UserId).key("IsGPSOn").value(String.valueOf(isServiceOn)).endObject();
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(vm.toString());
post.setEntity(entity);
HttpResponse response = client.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String vv = "";
while((vv = br.readLine()) != null){
Log.v("Response Count", vv+" "+UserId);
}
} catch (JSONException e1) {
e1.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebAPI 代码:
[HttpPut]
public int Put(SettingsVM vm)
{
using (var db = new ApiDBContext())
{
string sqlQry = "Select COUNT(ID) FROM Settings WHERE UserId= '" + vm.UserId + "'";
var count = db.Database.SqlQuery<Int32>(sqlQry).SingleOrDefault();
if (count != 0)
{
string sql = "Update Settings Set IsGPSOn={1} where UserId={0}";
count = db.Database.ExecuteSqlCommand(sql, new object[] { vm.UserId, vm.IsGPSOn });
db.SaveChanges();
}
else
{
string sql = "INSERT INTO Settings(UserId,IsGPSOn) VALUES({0},{1})";
count = db.Database.ExecuteSqlCommand(sql, new object[] { vm.UserId, vm.IsGPSOn });
db.SaveChanges();
}
return count;
}
}
我已经检查并测试了所有可能的解决方案来解决这个错误。但面临同样的问题。
谢谢