0

I want to write to an xml document as for every request to the URL , I want to add a parameter called “lname” and insert my last name as the value.

Any Suggestions?

Here is a snippet from my Main Activity.

public void loadPage() {
    new DownloadXmlTask().execute(URL);
}


private class DownloadXmlTask extends AsyncTask<String, Void, List<AdEntry>> {
    @Override
    protected List<AdEntry> doInBackground(String... urls) {
        try {
            return loadXmlFromNetwork(urls[0]);
        } catch (IOException e) {
            return null;
        } catch (XmlPullParserException e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(List<AdEntry> adEntries) {
       // Some code
    }
}

public List<AdEntry> loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
    InputStream stream = null;
    try {
        stream = downloadUrl(urlString);
        entryList = parse(stream);
    } finally {
        if (stream != null)
            stream.close();
    }
    return entryList;
}

public InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();
    return conn.getInputStream();
}
Laura
  • 73
  • 1
  • 6
  • welcome to stack overflow! my suggestion would be: https://stackoverflow.com/help/how-to-ask ...what have you tried so far? because as the question is currently written, it is too broad and does barely permit an accurate answer. and it's probably not about the `xmlpullparser`, which is used by Android for accessing XML resources. https://square.github.io/retrofit/ probably might rather be what you might be looking for. besides, one cannot post to an XML document, but in best case to an XML API. – Martin Zeitler Jan 03 '19 at 22:35
  • Thank you. I have already parsed the xml document and get all the data that I wanted and displayed them in the application. But at the same time I want to add a parameter called “lname” and insert my last name as the value for every request to the URL. So any suggestions of how to do that? I will post my some code. – Laura Jan 03 '19 at 22:38
  • with some code this would be way more specific and should be easy to answer; and we also have a guide for this: https://stackoverflow.com/help/mcve Q: do you need to `POST` parameter `lname` or does a `GET` suffice? – Martin Zeitler Jan 03 '19 at 22:40
  • This is how I got my data when parsing the xml document. But I have no idea how to do the post request part. I am new to xml – Laura Jan 03 '19 at 22:43
  • Yes, for every request to the URL , I want to add a parameter called “lname” and insert my last name as the value. – Laura Jan 03 '19 at 22:44
  • Possible duplicate of [Java - sending HTTP parameters via POST method easily](https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily) – Martin Zeitler Jan 03 '19 at 22:44
  • `HttpURLConnection` might not be the easiest way in general, because it's far more nuts and bolts than `retrofit2` would be - therefore one has to program much, which otherwise would be taken care of. – Martin Zeitler Jan 03 '19 at 22:48

0 Answers0