Kishan
Kishan

Reputation: 424

Retrieving xml stream from http server - android problem

In my applicaion, I have to fetch a list of some assignments from a server. On the server, i have a method,say fetchAssignments(int ,String), which takes two values as its parameters.

  1. Period ( today,current month or current week ) - integer
  2. user id - String

This function returns the list of assignments as an XML stream. I know how i can get connected to the http server. But i m not getting how i can invoke that method on the server and pass these parameters to it. Could anyone suggest me a better way of doing it...?

Upvotes: 1

Views: 853

Answers (2)

tutts
tutts

Reputation: 2433

Depending on the language you could pass the parameters through the URL HTTP request or using POST. In the constructor for the class (assuming its a webpage? .php? .aspx?) retrieve those values and pass them to the method mentioned above?

try{        
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/getfeed.php");
InputSource inStream = new InputSource();

try {
// Add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("period", period));
nameValuePairs.add(new BasicNameValuePair("userid", user_id));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

 // Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
String xmlString = EntityUtils.toString(r_entity);

xmlString = xmlString.trim();
InputStream in = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

if(xmlString.length() != 0){
inStream.setByteStream(in);
PARSE_FLAG = true;
}
else
{
PARSE_FLAG = false;
}

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

Upvotes: 1

Torsten R&#246;mer
Torsten R&#246;mer

Reputation: 3926

You could just request the XML as InputStream from the server using a HTTP GET request, and pass the parameters as request parameters:

http://some.server/webapp?period=1&userid=user1

With a method something like the below you can get the stream from the server:

/**
 * Returns an InputStream to read from the given HTTP url.
 * @param url
 * @return InputStream
 * @throws IOException
 */
public InputStream get(final String url) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpParams httpParams = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT);
    HttpGet httpget = new HttpGet(url);
    HttpResponse httpResponse = httpClient.execute(httpget);
    StatusLine statusLine = httpResponse.getStatusLine();
    if(! statusLine.getReasonPhrase().equals("OK")) {
        throw new IOException(String.format("Request failed with %s", statusLine));
    }
    HttpEntity entity = httpResponse.getEntity();
    return entity.getContent();
}

And then you could use the "Simple" (http://simple.sourceforge.net/) XML library to parse the XML into JAXB-like entities:

/**
 * Reads the XML from the given InputStream using "Simple" and returns a list of assignments.
 * @param InputStream
 * @return List<Assignment>
 */
public List<Assignment> readSimple(final InputStream inputStream) throws Exception {

    Serializer serializer = new Persister();

    return serializer.read(AssignmentList.class, inputStream).getAssignments();     
}

I am doing pretty much that, just with a REST service, so I don't use request parameters.

Upvotes: 3

Related Questions