离子返回XML错误而期望JSON

时间:2018-06-28 05:15:53

标签: java android ion

我正在使用Ion库将JsonArray发送到Web服务并获取JSOn输出。相同的url和输入在邮递员上可以很好地给出正确的结果。但是当提供相同的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&amp; rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; 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);
                }
            });

2 个答案:

答案 0 :(得分:1)

在简单的URI连接中,有用于请求的属性:

//our main app
var app = angular.module('users', [])

//our controller
function testController($scope) {
    function init() {
        //somthing
    }
    init();
}

testController.$inject = ['$scope'];
app.controller('testController', testController);

答案 1 :(得分:0)

离子不适用于我,因此我使用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));