0

Possible Duplicate:
How to reading XML from a URL in Android

The idea is to make an Android application, which would work with an existing server sending XML-messages and getting XML-responses.

Here's the code:

try {
                URL url1 = null;
                url1 = new URL("http://mysite.com/");
                URLConnection urlc1 = url1.openConnection();
                HttpURLConnection con1 = (HttpURLConnection) urlc1;

                con1.setRequestMethod("POST");

                con1.setUseCaches(false);
                con1.setDoOutput(true);
                con1.setDoInput(true);
                con1.setRequestProperty("accept-charset", "UTF-8");
                con1.setRequestProperty("content-type", "application/x-www-form-urlencoded");


                XmlSerializer ser1 = Xml.newSerializer();
                StringWriter writer = new StringWriter();
                ser1.setOutput(writer);
                ser1.startDocument("UTF-8", true);              
                ser1.startTag("", "request");
                ser1.attribute("", "query", "open_session");
                ser1.startTag("", "");
                ser1.attribute("", "client", "name");
                ser1.attribute("", "version", "1.0");
                ser1.endTag("", "");
                ser1.endTag("", "request");
                ser1.endDocument();

                String query = writer.toString();   

                con1.connect();

                OutputStreamWriter out1 = new OutputStreamWriter(con1.getOutputStream());  
                out1.write(query);  
                out1.flush();   


                String result1 = null;
                BufferedReader br1 = new BufferedReader(new InputStreamReader(con1.getInputStream()));
                 StringBuffer sb = new StringBuffer();
                 String line;
                 while ((line = br1.readLine()) != null){
                     sb.append(line);
                     }
                 br1.close();
                 result1 = sb.toString();


            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

The program opens the connection, creates a login request XML, sends it to the server and gets XML-answer.

But it does not work properly. The response I get is:

<html><head><title>302 Found</title></head></body bg color="white"><center><h1>302 Found</h1></center><hr><center>nginx/1.0.0</center></body><html>

And that's not a listed error from the server. The successful answer should be:

<result success="true">
<session-info time-out="number" id="number" start="number"/>
</result>

It's interesting, that when I comment this part of the code:

OutputStreamWriter out1 = new OutputStreamWriter(con1.getOutputStream());  
out1.write(query);  
out1.flush(); 

I get the proper XML-error answer:

<result success="false">
<error error-code="XML-01">
Wrong XML
</error>
</result>

Could you help me?

Thanks in advance.

4

1 回答 1

0

302 Found is a redirect message and if you are using the HttpClient, it can handle these redirects internally.

Take a look at https://stackoverflow.com/a/2560129/1321873 to get an overview of how you can post an XML to a server. (Although they are sending a SOAP XML, the mechanism for sending a plain XML is also the same - just change the content-type)

于 2012-10-22T12:42:49.133 回答