java得到不完整的肥皂反应

时间:2010-12-14 12:15:56

标签: java android soap

在我的项目中,我通过以这种格式发送soap请求来调用Web服务,

try {

       URL u = new URL(server);
       URLConnection uc = u.openConnection();
       HttpURLConnection connection = (HttpURLConnection) uc;

       connection.setDoOutput(true);
       connection.setDoInput(true);
       connection.setRequestProperty("SOAPAction", SOAP_ACTION);
       connection.setRequestMethod("POST");
       connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");


       String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
                        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+ 
                        "<soap:Body>"+
                        "<GetAllCars  xmlns=\"http://parkinghero.com/\">"+                          
                        "<clsGetAllCarsRequest>"+
                        "<clsCredentials>"+
                        "<EmailAddress>"+Email+"</EmailAddress>"+
                        "<Password>"+passWRD+"</Password>"+
                        "<TokenID>"+token+"</TokenID>"+
                        "</clsCredentials>"+
                        "</clsGetAllCarsRequest>"+
                          "</GetAllCars>"+"</soap:Body>"+
                        "</soap:Envelope>";        

       System.out.println(xmldata);

       OutputStream out = connection.getOutputStream();

          Writer wout = new OutputStreamWriter(out);

          wout.write(xmldata);

          wout.flush();

          wout.close();

          BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            //InputStream in = connection.getInputStream();

            String result;
        //int c;
          while ((result=rd.readLine()) != null) {

          System.out.println(result);

          int length = result.length();

          String jsonObj = result.substring(294, (length - 74));

          System.out.println(jsonObj);

          }rd.close();
   }
   catch (IOException e) 
   {
       System.out.println("in Exception called");

 System.err.println(e); 
   }

对于此请求,响应将为json array

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <soap:Body>
    <GetAllCarsResponse xmlns="http://abcd.com/">
    <GetAllCarsResult>
    {"clsError":{"ErrorCode":306,"ErrorDescription":"Data Available"},
    "CarsArray":[{"CarID":1,"UserID":1,"RegisteredOwner":"Suresh    thorata","Make":"Esteem","Model":"23434","Color":"Brownd","BodyType":"Normal","PlateType":"COM",
"PlateNumber":"WAG1PM","State":"NY","VINNumber":"000000000000","YearOfRegistration":2000,"ExpiryDate":"10/25/2014 4:24:57 PM",
"IsDeleted":true,"DateAddedTimeStamp":"10/18/2010 10:19:04 AM","LastUpdatedTimeStamp":"10/25/2010 4:24:57 PM"},
{"CarID":2,"UserID":1,"RegisteredOwner":"Balaji","Make":"Land Rover","Model":"9999","Color":"Brown","BodyType":"Normal","PlateType":"PAS","PlateNumber":"WAG2PM",
"State":"NY","VINNumber":"","YearOfRegistration":2003,"ExpiryDate":"10/18/2014 11:01:07 AM","IsAutoFightOn":true,"IsDeleted":true,"DateAddedTimeStamp":"10/18/2010
 11:01:07 AM","LastUpdatedTimeStamp":"10/18/2010 11:01:07 AM"},
 {"CarID":3,"UserID":1,"RegisteredOwner":"Suresh","Make":"Dodge","Model":"5670","Color":"Brown","BodyType":"Normal","PlateType":"AYG","PlateNumber":"WAG3PM",
 "State":"NY","VINNumber":"","YearOfRegistration":2000,"ExpiryDate":"10/18/2014 11:01:39 AM","IsDeleted":true,"DateAddedTimeStamp":"10/18/201
 0 11:01:39 AM","LastUpdatedTimeStamp":"12/9/2010 11:36:14 PM"},
 {"CarID":4,"UserID":1,"RegisteredOwner":"Suresh Babu","Make":"Infiniti","Model":"234353","Color":"Brownd","BodyType":"Normal","PlateType":"PAS",
 "PlateNumber":"WAG4PM","State":"NY","VINNumber":"000000000000","YearOfRegistration":2000,"ExpiryDate":"10/20/2014 1:01:22 PM","IsAutoFightOn":true,
 "IsDeleted":true,"DateAddedTimeStamp":"10/20/2010 1:01:22 PM","LastUpdatedTimeStamp":"12/3/2010 12:20:29 PM"},{"CarID":5,"UserID":1,
 "RegisteredOwner":"Suresh1 thorata","Make":"Esteem","Model":"23434","Color":"Brownd","BodyType":"Normal","PlateType":"COM",
 "PlateNumber":"WAG5PM","State":"NY","VINNumber":"000000000000","YearOfRegistration":2000,"ExpiryDate":"10/25/2014 11:44:06 AM"
 ,"IsDeleted":true,"DateAddedTimeStamp":"10/25/2010 11:39:00 AM","LastUpdatedTimeStamp":"10/25/2010 11:44:06 AM"},{"CarID":73,"UserID":1,
 "RegisteredOwner":"Balaji ","Make":"Audi","Model":"Sportz","Color":"Red","BodyType":"Coupe","PlateType":"PAS",
 "PlateNumber":"SUPERMAN","State":"NJ","VINNumber":"","YearOfRegistration":2010,"ExpiryDate":"12/12/2020 12:00:00 AM",
 "IsAutoFightOn":true,"IsDeleted":true,"DateAddedTimeStamp":"10/25/2010 6:36:07 PM","LastUpdatedTimeStamp":"12/9/2010 11:41:46 PM"},
 {"CarID":74,"UserID":1,"RegisteredOwner":"suresh","Make":"BMW","Model":"sedane","Color":"silver","BodyType":"Convertible","PlateType":"PAS",
 "PlateNumber":"GREENMAN","State":"NJ","VINNumber":"","YearOfRegistration":2010,"ExpiryDate":"12/12/2020 12:00:00 AM","IsAutoFightOn":true,"IsDeleted":false,
 "DateAddedTimeStamp":"10/26/2010 10:15:34 AM","LastUpdatedTimeStamp":"12/9/2010 11:38:56 PM"},{"CarID":79,"UserID":1,"RegisteredOwner":"Balu","Make":"Hero",
 "Model":"Honda","Color":"Green","BodyType":"2005","PlateType":"ARG - Air National Guard","PlateNumber":"2434","State":"NY","VINNumber":"12345",
 "YearOfRegistration":2004,"ExpiryDate":"3/17/2021 12:00:00 AM","IsAutoFightOn":false,"IsDeleted":false,"DateAddedTimeStamp":"11/17/2010 9:55:32 AM",
 "LastUpdatedTimeStamp":"12/6/2010 11:13:01 AM"},{"CarID":80,"UserID":1,"RegisteredOwner":"Balaji","Make":"Accura","Model":"i10","Color":"Red",
 "BodyType":"1994","PlateType":"ATD - All Terrain Dealer","PlateNumber":"NOFEAR","State":"DE","VINNumber":"456789","YearOfRegistration":1999,
 "ExpiryDate":"10/29/2047 12:00:00 AM","IsAutoFightOn":true,"IsDeleted":false,"DateAddedTimeStamp":"11/17/2010 11:25:54 AM",
 "LastUpdatedTimeStamp":"11/17/2010 11:25:54 AM"},{"CarID":81,"UserID":1,"RegisteredOwner":"Balaji","Make":"Esteem","Model":"Honda","Color":"LemonYellow",
 "BodyType":"1976","PlateType":"AYG","P

我能得到回复,但不完整。我究竟做错了什么。我认为这是因为缓冲读卡器。

1 个答案:

答案 0 :(得分:0)

@Kishore:这是代码

String SOAP_ACTION = "service soap action";
         try {
            URL u = new URL("server url");
            URLConnection uc = u.openConnection();
            HttpURLConnection connection = (HttpURLConnection) uc;
            connection.setDoOutput(true);
               connection.setDoInput(true);
               connection.setRequestProperty("SOAPAction", SOAP_ACTION);
               connection.setRequestMethod("POST");
               connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
String xmldata="soap request envelope";
//send the request
               OutputStream out = connection.getOutputStream();

                  Writer wout = new OutputStreamWriter(out);

                  wout.write(xmldata);

                  wout.flush();

                  wout.close();

                  BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));



                    String result;
                    StringBuilder builder=new StringBuilder();
                            //read response
                    while ((result=rd.readLine()) != null) {
                        builder.append(result);
                    }

感谢您的帮助