从http服务器检索xml流 - android问题

发布于 2024-11-25 12:43:17 字数 245 浏览 3 评论 0原文

在我的应用程序中,我必须从服务器获取一些作业的列表。在服务器上,我有一个方法,例如 fetchAssignments(int ,String),它采用两个值作为其参数。

  1. 期间(今天、当前月份或本周) - 整数
  2. 用户 ID - 字符串

此函数以 XML 流的形式返回作业列表。我知道如何连接到 http 服务器。但我不知道如何在服务器上调用该方法并将这些参数传递给它。谁能建议我更好的方法......?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2024-12-02 12:43:17

您可以使用 HTTP GET 请求从服务器请求 XML 作为 InputStream,并将参数作为请求参数传递:

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

使用类似下面的方法,您可以从服务器获取流:

/**
 * 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();
}

然后您可以使用“Simple” (http://simple.sourceforge.net/) XML 库,用于将 XML 解析为类似 JAXB 的实体:

/**
 * 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();     
}

我正在这样做,只是使用 REST 服务,所以我不使用请求参数。

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.

烟花肆意 2024-12-02 12:43:17

根据语言,您可以通过 URL HTTP 请求或使用 POST 传递参数。在类的构造函数中(假设它是一个网页?.php?.aspx?)检索这些值并将它们传递给上面提到的方法?

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文