简体   繁体   English

访问Web服务的返回值

[英]Access Webservice return value

I have a webservice which returns an arraylist of objects. 我有一个Web服务,它返回对象的数组列表。 I want to access this arraylist of objects in my android code. 我想在我的android代码中访问此对象的arraylist。 How do i do it? 我该怎么做? My webservice is 我的网络服务是

package com.chillum.first;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

@Path("/hello")
public class ServerSide {

@GET
public ArrayList<myClass> Print (@QueryParam("param1") String key) throws IOException, InterruptedException
{
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyyhmmss");
    String formattedDate = sdf.format(date);
    String command = "cmd /c start /wait C:\\python27\\tutorial\\check.bat "+key+" "+formattedDate;
    Process p=Runtime.getRuntime().exec(command);
    p.waitFor();
    myClass[] flipkart=new myClass[500];
    myClass[] amazon=new myClass[500];
    myClass[] myntra=new myClass[500];
    myClass[] jabong=new myClass[500];
    myClass[] shopclues=new myClass[500];
    myClass[] hs18=new myClass[500];
    myClass[] indiatimes=new myClass[500];
    myClass[] croma=new myClass[500];

    String xmlFileNames[]=new String[8];
    xmlFileNames[0]="C:/python27/tutorial/"+formattedDate+"flipkart.xml";
    xmlFileNames[1]="C:/python27/tutorial/"+formattedDate+"amazon.xml";
    xmlFileNames[2]="C:/python27/tutorial/"+formattedDate+"myntra.xml";
    xmlFileNames[3]="C:/python27/tutorial/"+formattedDate+"jabong.xml";
    xmlFileNames[4]="C:/python27/tutorial/"+formattedDate+"shopclues.xml";
    xmlFileNames[5]="C:/python27/tutorial/"+formattedDate+"hs18.xml";
    xmlFileNames[6]="C:/python27/tutorial/"+formattedDate+"indiatimes.xml";
    xmlFileNames[7]="C:/python27/tutorial/"+formattedDate+"croma.xml";

    String[] domain=new String[8];
    domain[0]="http://www.flipkart.com";
    domain[1]="";
    domain[2]="http://www.myntra.com";
    domain[3]="";
    domain[4]="http://www.shopclues.com";
    domain[5]="http://www.homeshop18.com";
    domain[6]="http://www.shopping.indiatimes.com";
    domain[7]="http://www.cromaretail.com/";


    try {
        random(flipkart, xmlFileNames[0], domain[0]);
        random(amazon, xmlFileNames[1], domain[1]);
        random(myntra, xmlFileNames[2], domain[2]);
        random(jabong, xmlFileNames[3], domain[3]);
        random(shopclues, xmlFileNames[4], domain[4]);
        random(hs18, xmlFileNames[5], domain[5]);
        random(indiatimes, xmlFileNames[6], domain[6]);
        random(croma, xmlFileNames[7], domain[7]);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ArrayList<myClass> all= new ArrayList<myClass>();
    for(int i=0, j=0; i<50; i++, j++)
    {
        try
        {
            all.add(flipkart[i]);
            all.add(amazon[i]);
            all.add(myntra[i]);
            all.add(jabong[i]);
            all.add(shopclues[i]);
            all.add(hs18[i]);
            all.add(indiatimes[i]);
            all.add(croma[i]);
        }
        catch(NullPointerException npe)
        {
        }
            continue;
    }
    return all;
}
public static void random(myClass[] obj, String fileName, String domain) throws Exception
{
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    try
    {
        Document document = builder.parse(new InputSource(new FileReader( new File(fileName))));
        NodeList title = document.getElementsByTagName("title");
        NodeList price = document.getElementsByTagName("price");
        NodeList link = document.getElementsByTagName("link");
        NodeList image = document.getElementsByTagName("image");
        for (int i = 0; i < title.getLength(); i++) {
            int objcount=0;
            NodeList titleChildList = title.item(i).getChildNodes();
            NodeList priceChildList = price.item(i).getChildNodes();
            NodeList linkChildList = link.item(i).getChildNodes();
            NodeList imageChildList = image.item(i).getChildNodes();
            for (int j = 0; j < titleChildList.getLength(); j++) {
                obj[objcount]=new myClass();
                Node titleChildNode = titleChildList.item(j);
                if ("value".equals(titleChildNode.getNodeName())) {

                    obj[objcount].title=titleChildList.item(j).getTextContent()
                            .trim();
                }
                Node priceChildNode = priceChildList.item(j);
                if ("value".equals(priceChildNode.getNodeName())) {
                    obj[objcount].price=priceChildList.item(j).getTextContent()
                            .trim();
                }   
                Node linkChildNode = linkChildList.item(j);
                if ("value".equals(linkChildNode.getNodeName())) {
                    obj[objcount].link=domain+linkChildList.item(j).getTextContent()
                        .trim();
                }
                Node imageChildNode = imageChildList.item(j);
                if ("value".equals(imageChildNode.getNodeName())) {
                    obj[objcount].image=imageChildList.item(j).getTextContent()
                        .trim();
                }
                objcount++;
            }
        }   
    }
    catch(NullPointerException npe) {
    }

}

}

And my java class which returns to android is 我返回到android的java类是

package com.example.chillum;

import android.net.http.AndroidHttpClient;

import com.example.chillum.Utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;

public class HttpManager {

public static ArrayList<myClass> getdata(String uri)
{
    AndroidHttpClient client=AndroidHttpClient.newInstance("AndroidAgent");
    HttpGet request=new HttpGet(uri);
    HttpResponse response;
    try{
        response=client.execute(request);
        return //what?;
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return null;
    }
    finally
    {
        client.close();
    }
}
}

What should i return such that i get the array list? 我应该返回什么以获取阵列列表? This methods returns to an async task where the other operations are to be done with the arraylist. 此方法返回到异步任务,其中其他操作将通过arraylist完成。 please help. 请帮忙。

You can't transfer a Java Object from the server to the client using HTTP. 您无法使用HTTP将Java对象从服务器传输到客户端。 HTTP uses basic ASCII text to transfer data (hence Hyper Text Transfer Protocol). HTTP使用基本的ASCII文本来传输数据(因此称为超文本传输协议)。

The workaround that needs to be used is using a standard markup language to encode the data you are sending from the server. 需要使用的解决方法是使用标准标记语言对从服务器发送的数据进行编码。 The major ones that are used right now are JSON and XML. 目前主要使用的是JSON和XML。

JSON looks like this : JSON看起来像这样:

{ 
"company": Volkswagen,
"name": "Vento",
"price": 800000
}

And XML Looks like this : XML看起来像这样:

<car>
<company>Volkswagen</company>
<name>Vento</name>
<price>800000</price>
</car>

In simple terms: You will have to convert the Object you want to send to JSON/XML before emitting to the client request. 简单来说:在发送给客户端请求之前,您必须将要发送的对象转换为JSON / XML。 Then use a JSON decode function at the client side to get back the object. 然后,在客户端使用JSON解码功能取回该对象。

There are quite a good number of tutorials to show how to use JSON/XML to transfer data between server and Android Activity. 有大量的教程来展示如何使用JSON / XML在服务器和Android Activity之间传输数据。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM