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

Apps XML data from url with POST method

Hello there!

I have an API I'm trying to get somewhere with.
In the API documentation it says that I have to make a http call with a question with the POST method about what information I want.

What i'm trying to get from the homepage is accessing an xml file. It's provided with a url.

Sooo, can someone just guide me to where I can actually read about http calls with a question via the POST method? I must suck at googling or I'm not searching for the right stuff.

Thanks in advance.
 
No problem. Try some of those examples. If you run into problems feel free to ask further questions here.
 
I tried my hands on Volley.

I setup a textfile with <?php with just an echo that gives me a message back.
with volley I used string url = "http://myip/filename.php" and it works like charm.

The API i'm trying to talk to wants me to use their url, of course, http://api.trafikinfo.trafikverket.se/v[version]/data.[format], and the format I want to use is xml.
In their documentation they say I have to make a request. The format they are using is:
<REQUEST>
<LOGIN authenticationkey="SomeAuthenticationKey" />
<QUERY objecttype="SomeObjectType" >
<FILTER>
<EQ name="SomeDataField" value="2" />
</FILTER>
</QUERY>
</REQUEST>

What I have not found out is to send a custom request like this one to the site.
The request will ask for a train number for example and give back information about the train with that number. Departure etc etc.

Any clues or tips on how to do that?
Thanks in advance.
 
Yeah been trying now for a bit. Seems to be the request content-type that is the problem.

I get "unsupported request content type" as an answer back.
I've tried "application/xml", "application/x-www-form-urlencoded", "text/xml" etc with the overrided getBodyContentType function.

According to postman the url is "Content-Type →text/xml; charset=utf-8"
If I try that content-type I will just go in to onErrorResponse(). Dunno what i'm doing wrong.
Can you see something that I might have missed, or completly missunderstood?

Code:
public class MainActivity extends AppCompatActivity {
    Button button;
    TextView textView;
    String server_url = "http://api.trafikinfo.trafikverket.se/v1.1/data.xml";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.txt);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

                StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,

                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                textView.setText(response);
                                requestQueue.stop();
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        textView.setText("Something went wrong.");
                        error.printStackTrace();
                        requestQueue.stop();
                    }


                })

                {
                    //Tried this because someone said to try it.
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<String, String>();

                        return headers;
                    }

                    //The content-type seems to be the problem.
                    @Override
                    public String getBodyContentType() {
                    return "apllication/xml;" +
                            getParamsEncoding();
                }

                    //Seems to be correct.
                    @Override
                    public byte[] getBody() throws AuthFailureError {
                    String postData = "<REQUEST version='1.0'>" +
                            "<LOGIN authenticationkey='ledsagarapp' />" +
                            "<QUERY objecttype='TrainAnnouncement' limit='1'> " +
                            "<FILTER>" +

                            "</FILTER>" +
                            "<INCLUDE>TechnicalTrainIdent</INCLUDE>" +
                            "</QUERY>" +
                            "</REQUEST>"; // TODO get your final output
                    try {
                        return postData == null ? null :
                                postData.getBytes(getParamsEncoding());
                    } catch (UnsupportedEncodingException uee) {
                        // TODO consider if some other action should be taken
                        return null;
                    }
                }


                };
                requestQueue.add(stringRequest);
            }
        });
    }

}
 
Further testing show that I cannot use the name of the key on <LOGIN authenticationkey='ledsagarapp' />, have to use the actual value of the key. Weird, they don't do that in the API documentation.

With that change I can use text/xml to get the data I wanted. Sweet!
 
One question, I guess I have to use a parser on the respons to get out the data I asked for instead of the entire repsonse?

DOM, SAX etc
 
Back
Top Bottom