我试图在一周内解决此错误,但我的任何解决方法都不能正常工作
我正在尝试使用volley库post方法将类似于String = {“name”:“ABC”}的json String发送到WCF(ASP.NET),但它无法正常工作会出现此错误
BasicNetwork.performRequest:http://192.168.1.11/MyDemoService/Service1.svc/insert
的意外响应代码400我不确定出了什么问题,错误在哪里
以下是WCF代码:
[WebInvoke(Method = "POST", UriTemplate = "insert", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string insert(string name);
插入功能:
public string insert(string name)
{
//write database related insert logic here.
//string response2="{\"name\":\"mohib\"}";
//info m = JsonConvert.DeserializeObject<info>(name);
//string n = m.name;
// string ds = name;
string conStr = @"Data Source=CRESIDIAN-DELL;Initial Catalog=WCFTest;User Id=sa;Password=1234";
// string msg = "true";
try
{
SqlConnection con = new SqlConnection(conStr);
con.Open();
string query = "insert into DemoTable (name) values ('" + name + "')";
SqlCommand com = new SqlCommand(query, con);
com.ExecuteNonQuery();
msg = "Inserted";
con.Close();
}
catch (Exception e)
{
e.ToString();
}
return msg;
}
WCF网络配置:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services >
<service name="WCFDemo.Service1">
<endpoint address="" binding="webHttpBinding" contract="WCFDemo.IService1" behaviorConfiguration="MyConfig">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors >
<behavior name="MyConfig">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Android(客户端)中的排球代码:
String url="http://192.168.1.11/MyDemoService/Service1.svc/insert";
JSONObject params = new JSONObject();
try {
params.put("name", "true");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//Log.d(TAG, response.toString());
Log.d("Tag",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
BaseApplication.getInstance().addToRequestQueue(jsonObjReq,"Post Request");