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

从http服务器检索xml流-android问题,android,xml,http,stream,connection,Android,Xml,Http,Stream,Connection,在我的应用程序中,我必须从服务器获取一些任务的列表。在服务器上,我有一个方法,比如fetchAssignments(int,String),它采用两个值作为参数 期间(今天、本月或本周)-整数 用户id-字符串 此函数以XML流的形式返回分配列表。我知道如何连接到http服务器。但我不知道如何在服务器上调用该方法并将这些参数传递给它。有谁能给我建议一个更好的方法吗 您可以使用HTTP GET请求从服务器请求XML作为InputStream,并将参数作为请求参数传递: http://some.se

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

  • 期间(今天、本月或本周)-整数
  • 用户id-字符串

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

    您可以使用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();
    }
    
    然后你可以用“简单”(http://simple.sourceforge.net/)将XML解析为类似JAXB的实体的XML库:

    /**
     * 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();     
    }
    
    /**
    *使用“Simple”从给定的InputStream读取XML,并返回赋值列表。
    *@param InputStream
    *@返回列表
    */
    公共列表readSimple(最终InputStream InputStream)引发异常{
    Serializer Serializer=new Persister();
    返回serializer.read(AssignmentList.class,inputStream).getAssignments();
    }
    

    我使用REST服务就可以做到这一点,所以我不使用请求参数。

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

    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();
    }
    
    然后你可以用“简单”(http://simple.sourceforge.net/)将XML解析为类似JAXB的实体的XML库:

    /**
     * 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();     
    }
    
    /**
    *使用“Simple”从给定的InputStream读取XML,并返回赋值列表。
    *@param InputStream
    *@返回列表
    */
    公共列表readSimple(最终InputStream InputStream)引发异常{
    Serializer Serializer=new Persister();
    返回serializer.read(AssignmentList.class,inputStream).getAssignments();
    }
    

    我只是通过REST服务来实现这一点,所以我不使用请求参数。

    根据您可以通过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
    }
    
    试试{
    HttpClient HttpClient=新的DefaultHttpClient();
    HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/getfeed.php");
    InputSource inStream=新的InputSource();
    试一试{
    //添加数据
    List nameValuePairs=新的ArrayList(2);
    添加(新的BasicNameValuePair(“期间”,period));
    添加(新的BasicNameValuePair(“userid”,user_id));
    setEntity(新的UrlEncodedFormEntity(nameValuePairs));
    //执行HTTP Post请求
    HttpResponse response=httpclient.execute(httppost);
    HttpEntity r_entity=response.getEntity();
    String xmlString=EntityUtils.toString(r_entity);
    xmlString=xmlString.trim();
    InputStream in=newbytearrayinputstream(xmlString.getBytes(“UTF-8”);
    如果(xmlString.length()!=0){
    河道内尾流(in);
    PARSE_FLAG=true;
    }
    其他的
    {
    PARSE_FLAG=false;
    }
    }捕获(客户端协议例外e){
    //TODO自动生成的捕捉块
    }捕获(IOE异常){
    //TODO自动生成的捕捉块
    }
    
    根据您可以通过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
    }
    
    试试{
    HttpClient HttpClient=新的DefaultHttpClient();
    HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/getfeed.php");
    InputSource inStream=新的InputSource();
    试一试{
    //添加数据
    List nameValuePairs=新的ArrayList(2);
    添加(新的BasicNameValuePair(“期间”,period));
    添加(新的BasicNameValuePair(“userid”,user_id));
    setEntity(新的UrlEncodedFormEntity(nameValuePairs));
    //执行HTTP Post请求
    HttpResponse response=httpclient.execute(httppost);
    HttpEntity r_entity=response.getEntity();
    String xmlString=EntityUtils.toString(r_entity);
    xmlString=xmlString.trim();
    InputStream in=newbytearrayinputstream(xmlString.getBytes(“UTF-8”);
    如果(xmlString.length()!=0){
    河道内尾流(in);
    PARSE_FLAG=true;
    }
    其他的
    {
    PARSE_FLAG=false;
    }
    }捕获(客户端协议例外e){
    //TODO自动生成的捕捉块
    }捕获(IOE异常){
    //TODO自动生成的捕捉块
    }
    
    HTTP POST如果受支持会更好,+1 back:-)如果参数匹配,它是否调用服务器上的相应函数…?正如我所说,我需要调用fetchAssignment(int,string)在服务器上…您如何在服务器上调用该函数?不是通过HTTP?JMX,RMI?仅通过HTTP,但我不知道如何调用。您的代码是否调用该函数?或者在服务器上调用该函数是否足够?如果您使用HTTP,那么我想您在该服务器上运行了一些web服务,这些服务实现GET或POST或两者都有。然后这取决于您拥有哪种服务:Servlet、SOAP或REST Web服务,…您需要根据这些服务进行调用。如果支持HTTP POST,则更好,+1 back:-)如果参数匹配,它是否调用服务器上的相应函数…?正如我所说,我需要调用fetchAssignment(int,string)在服务器上…您如何在服务器上调用该函数?不是通过HTTP?JMX,RMI?仅通过HTTP,但我不知道如何调用。您的代码是否调用该函数?或者在服务器上调用该函数是否足够?如果您使用HTTP,那么我想您在该服务器上运行了一些web服务,这些服务实现GET或POST或两者都有。然后这取决于您拥有哪种服务:Servlet、SOAP或REST Web服务……您需要根据这些服务进行调用。