• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

XML post to HTTP not working properly

JonasJWW

Newbie
Hi guys,

So I´m trying to develop an App to control a thermostat. I´m using an API provided by the manufacturer. Here´s a description. I´m already reading an XML from the URL and now I need to post an XML file to a HTTP. I have no Idea how to do this and searched for some solutions. This is my current PostClass method:

Code:
public class PushData extends AsyncTask{

private final String postUrl = "http://192.168.178.38/data/changes.xml";
private final String postString = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
        " <Devices>\n" +
        " <Device>\n" +
        " <ID>EZR0114AF</ID>\n" +
        " <HEATAREA nr=\"4\">\n" +
        " <T_TARGET>18.7</T_TARGET>\n" +
        " </HEATAREA>\n" +
        " </Device>\n" +
        " </Devices>";

@Override
protected Object doInBackground(Object[] objects) {

    try {
        URL url = new URL(postUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        String body = postString;
        OutputStream output = new BufferedOutputStream(conn.getOutputStream());
        output.write(body.getBytes());
        output.flush();

        Log.d("Success", postString);

    } catch (ProtocolException e) {
        e.printStackTrace();

    } catch (IOException e1) {
        e1.printStackTrace();

    } finally {

    }

    return null;
}
}

In my LogCat it says "Success", ... so my Code seems to be working at first. However when I check the xml file in my Browser it doesn't display the changes. So I´m not sure if the problem is in my Code or if the API is not working properly. Also I´m the API should be sending a respons after sending a post but I don't know how to get this response. Any help on how I can get the push to work and getting the response would be great!
 
You are trying to implement custom HTTP connection class. And you have problem with it. I've provided you working example with Volley library that can help you with your primary goal and not diving into implementation of connection service.
 
I've read the PDF file describing the interface. You appear to have constructed the XML correctly, and are POSTing the command to the base URL, in order to instruct the room temperature control unit.

To try and answer your specific questions:-

However when I check the xml file in my Browser it doesn't display the changes

I'm unsure what you are trying to check here. The purpose of your HTTP POST request is to send an instruction to the base controller. Is there some web interface for this you're trying to check, to verify that the command has been received?

Also I´m the API should be sending a respons after sending a post but I don't know how to get this response

It's possible to send information within the HTTP response body, but the PDF document doesn't mention anything about this. Can you explain what response you're expecting, other than the standard HTTP 200 (Ok) response code?
The interface description document didn't really contain any information about this, unless I've missed something.
 
I'm unsure what you are trying to check here. The purpose of your HTTP POST request is to send an instruction to the base controller. Is there some web interface for this you're trying to check, to verify that the command has been received?

Yes there is a web interface where I can check the status. Also under http ://<MyIP>/data/static.xml I can see the xml file which I´m reading from and under <HEATAREA nr=\"4\"> there are no changes to <T_TARGET>.

It's possible to send information within the HTTP response body, but the PDF document doesn't mention anything about this. Can you explain what response you're expecting, other than the standard HTTP 200 (Ok) response code?
The interface description document didn't really contain any information about this, unless I've missed something.

I just saw that this part seems to not be covered in the English description. In the German version it also says something like: As an response to the command you also receive an XML File with the corresponding responsecode:

<?xml version="1.0" encoding="UTF-8"?>

<COMMANDRESPONSE>
<STATE>OK</STATE>
<ERRORNUMBER>0</ERRORNUMBER>

</COMMANDRESPONSE>
 
You are trying to implement custom HTTP connection class. And you have problem with it. I've provided you working example with Volley library that can help you with your primary goal and not diving into implementation of connection service.

I can't really figure out what you are doing in this Project. I guess the for me interesting part is in your Partietache.java class? Where do I post the XML file there?
 
Back
Top Bottom