0
我有一個WCF服務,當我嘗試發送一個JSON後,我的操作合同有一個參數,我收到400個錯誤的請求。WCF後操作合同返回400錯誤的請求對於Android請求
Obs:如果我沒有這樣的操作合同參數。相同的請求完美地工作。
[OperationContract] //無參數工作正常。 bool AddCadastroJSon();
[OperationContract] bool AddCadastroJSon(String json); //帶有參數400錯誤請求。
我的合同
namespace CadastroTelefonesService
{
[ServiceContract]
public interface ICadastro
{
[WebInvoke(Method = "GET", ResponseFormat =WebMessageFormat.Json,UriTemplate = "addcliente/{nome};{telefone}")]
[OperationContract]
bool AddCadastro(String nome, String telefone);
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "listacliente")]
[OperationContract]
List<Cadastro> Lista();
[WebInvoke(Method = "POST",ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,UriTemplate = "addclientejson")]
[OperationContract]
bool AddCadastroJSon(String cadastro);
}
}
我HttpManager在Android中
/**
* Created by Gabriel Santana on 8/26/2015.
* Classe Responsável por fazer requisições HTTP e retornar um Request Package com o content.
*
*/
public class HttpManager {
public static String getData(RequestPackage p) {
BufferedReader reader = null;
HttpURLConnection con=null;
//OkHttpClient client=null;
URL url;
String uri = p.getUri();
if (p.getMethod().equals("GET")) {
uri += "?" + p.getEncodedParams();
}
try {
url = new URL(uri);
con = (HttpURLConnection) url.openConnection();
//client = new OkHttpClient();
// con = client.open(url);
con.setRequestMethod(p.getMethod());
JSONObject json = new JSONObject(p.getParams());
String params = "params="+json.toString();
if (p.getMethod().equals("POST")) {
con.setDoOutput(true);
con.setRequestProperty("Accept" , "application/json");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
//OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(params);
writer.flush();
writer.close();
os.close();
}
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.i("fudeu",con.getResponseMessage()+con.getResponseCode());
return sb.toString();
} catch (Exception e) {
try {
Log.i("fudeu",con.getResponseMessage()+con.getResponseCode());
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}
我一直在嘗試了3天,但我無法解決這個問題。