1

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...?

dogbane
  • 242,394
  • 72
  • 372
  • 395
Kishan
  • 449
  • 5
  • 20

2 Answers2

3

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.

Torsten Römer
  • 3,644
  • 3
  • 36
  • 48
  • HTTP POST is better if supported, +1 back :-) – Torsten Römer Jul 19 '11 at 12:50
  • If parameter match, does it call the respective function on the server....? As i said, i need to invoke fetchAssignment(int, string) on the server... – Kishan Jul 24 '11 at 08:39
  • How are you calling that function on the server? Not via HTTP? JMX, RMI? – Torsten Römer Jul 24 '11 at 21:35
  • Via http only, but i dont know how to call. Does your code invoke the function? Or is it enough to invoke that function on the server? – Kishan Jul 25 '11 at 10:28
  • 1
    If you are using HTTP, then I suppose you have some web service running on that server, that implements either GET or POST or both. Then it depends on what kind of service you have: A Servlet, SOAP or REST webservice, ... you need to make your call according to that. – Torsten Römer Jul 25 '11 at 20:49
  • I dont know all the things u just told, is there any tutorial or something...? So that i can first study those things... – Kishan Jul 27 '11 at 10:33
  • The above code was written assuming server implements GET... right? But what are these things, servlet, soap .....? – Kishan Jul 27 '11 at 10:36
  • Yes, I assumed you have some HTTP web service running. You could ask whoever takes care of the server you want to make your request to what service it provides and what you can use to talk to it. Have a look in Wikipedia for "Servlet", "SOAP" and "REST" for a starting point. – Torsten Römer Jul 27 '11 at 12:24
  • Which web service component does your code use, servelet or SOAP...? the above code... – Kishan Jul 27 '11 at 16:14
  • The above example could be used for a servlet. It can get the parameters `period` and `userid` from the request and then call the method `fetchAssignments(int ,String)` and return the XML back to the client. – Torsten Römer Jul 28 '11 at 08:58
  • I just got to know our server uses SOAP, could you tell me , with a small code snippet, how i can send a SOAP request with 2 parameters and invoke fetchAssignments(int , string)..? – Kishan Aug 02 '11 at 08:56
  • For SOAP you need to use a SOAP client for Android, as you can't (or you shouldn't) create/read SOAP requests/responses manually. Have a look at this [stackoverflow](http://stackoverflow.com/questions/297586/how-to-call-soap-web-service-with-android) - I am sure you can find something there. – Torsten Römer Aug 02 '11 at 20:32
1

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
}
tutts
  • 2,083
  • 1
  • 19
  • 24