Ion 返回 XML 错误而期望 JSON
Ion returning XML error whereas expecting JSON
我正在使用 Ion 库将 JsonArray 发送到 Web 服务并获得 JSOn 输出。相同的 url 和 and 输入在邮递员身上运行良好,给出了正确的结果。但是当提供相同的 url 并通过 Ion 库输入时,它会以 xml 格式给出请求错误。
请按照我正在使用的代码帮助我摆脱这个问题
Ion.with(PracticalExam.this)
.load(url)
.setJsonArrayBody(jsonArray)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result)
{
String error=null;
System.out.println("RESPONSE===> "+result);
}
});
以下是我得到的回复
`���<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Request Error</p>
<p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://json.ezeetest.in/EZEEService.svc/help">service help page</a> for constructing valid requests to the service. The exception message is 'Incoming message for operation 'PracticalResult' (contract 'IEZEEService' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p>
<p> at System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(Message message, Boolean isRequest)
at System.ServiceModel.Dispatcher.HttpStreamFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
</div>
</body>
</html>
`
我尝试按照建议进行操作,但仍然得到空结果
Ion.with(PracticalExam.this)
.load("POST",url)
.addQuery("action", "dummyAction")
.addHeader("Content-Type", "application/json")
.setJsonArrayBody(jsonArray)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>()
{
@Override
public void onCompleted(Exception e, JsonObject result)
{
pd.dismiss();
System.out.println("Result=> " + result);
System.out.println("Exception=> "+e);
}
});
在简单的 URI 连接中有 属性 用于请求:
setRequestProperty("Content-Type", "application/json");
Ion 对我不起作用,因此我使用 AsyncTask 做到了,下面是代码
public class SendJsonArrayToServer extends AsyncTask<String, String, String>
{
String jsonData;
private String JsonResponse;
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Saving...");
pd.show();
}
@Override
protected String doInBackground(String... params)
{
String jsonResponse = null;
String jsonData = params[0];
try
{
URL url = new URL("http://xxxxxxxxxxxxx/xxxxxxxxxxxxx");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(jsonData);
writer.close();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine + "\n");
if (buffer.length() == 0)
{
// Stream was empty. No point in parsing.
return null;
}
JsonResponse = buffer.toString();
//send to post execute
return JsonResponse;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
try {
urlConnection.disconnect();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
pd.dismiss();
}
并使用
调用AsyncTask
new SendJsonArrayToServer().execute(String.valueOf(jsonArray));
我正在使用 Ion 库将 JsonArray 发送到 Web 服务并获得 JSOn 输出。相同的 url 和 and 输入在邮递员身上运行良好,给出了正确的结果。但是当提供相同的 url 并通过 Ion 库输入时,它会以 xml 格式给出请求错误。
请按照我正在使用的代码帮助我摆脱这个问题
Ion.with(PracticalExam.this)
.load(url)
.setJsonArrayBody(jsonArray)
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result)
{
String error=null;
System.out.println("RESPONSE===> "+result);
}
});
以下是我得到的回复
`���<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Request Error</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Request Error</p>
<p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://json.ezeetest.in/EZEEService.svc/help">service help page</a> for constructing valid requests to the service. The exception message is 'Incoming message for operation 'PracticalResult' (contract 'IEZEEService' with namespace 'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is: </p>
<p> at System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(Message message, Boolean isRequest)
at System.ServiceModel.Dispatcher.HttpStreamFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
</div>
</body>
</html>
`
我尝试按照建议进行操作,但仍然得到空结果
Ion.with(PracticalExam.this)
.load("POST",url)
.addQuery("action", "dummyAction")
.addHeader("Content-Type", "application/json")
.setJsonArrayBody(jsonArray)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>()
{
@Override
public void onCompleted(Exception e, JsonObject result)
{
pd.dismiss();
System.out.println("Result=> " + result);
System.out.println("Exception=> "+e);
}
});
在简单的 URI 连接中有 属性 用于请求:
setRequestProperty("Content-Type", "application/json");
Ion 对我不起作用,因此我使用 AsyncTask 做到了,下面是代码
public class SendJsonArrayToServer extends AsyncTask<String, String, String>
{
String jsonData;
private String JsonResponse;
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Saving...");
pd.show();
}
@Override
protected String doInBackground(String... params)
{
String jsonResponse = null;
String jsonData = params[0];
try
{
URL url = new URL("http://xxxxxxxxxxxxx/xxxxxxxxxxxxx");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(jsonData);
writer.close();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine + "\n");
if (buffer.length() == 0)
{
// Stream was empty. No point in parsing.
return null;
}
JsonResponse = buffer.toString();
//send to post execute
return JsonResponse;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
try {
urlConnection.disconnect();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
pd.dismiss();
}
并使用
调用AsyncTasknew SendJsonArrayToServer().execute(String.valueOf(jsonArray));