需要一个简单的教程为Android / web服务工作?简单、教程、工作、Android

2023-09-13 01:22:42 作者:羡慕许仙日过蛇、

我真的很新的与Android合作,所以有很多是令人困惑我。我看什么似乎像100教程和如何从Android上的Web服务信息的例子,但我需要的是东西,它没有任何线索的人。这里有几件事情,特别是我没有变:

I'm really new working with Android, so there's a lot that's confusing me. I've looked at what seems like 100 tutorials and examples of how to get information from a web service on Android, but what I need is something for a guy that doesn't have a clue. Here are a couple of things in particular that I'm not getting:

在我不知道如何处理XML文件..意义,一旦我做了Java的工作,是所有需要做什么?还是什么需要在XML文件进行更改? 好像,也许我应该创造一些这些教程一个新的类,但我不知道,如果是的话,我不知道这样做,一旦我做了类 我要检索JSON格式的信息。对于现在只要我能得到公正的信息,这很好,我可以学习如何使用JSON更高版本。 好像kSoap2是做到这一点的最好办法。我有真实需要与它合作的jar文件 在我钻研得到PhoneGap的,所以如果有,它使用一个答案的话,我可以与工作

我的Web服务是否正常工作,并在本质上是一样的我所看到的一些教程,所以没有问题的。

My web service is working properly, and is essentially the same as what I've seen in a number of tutorials, so there's no problem there.

如果任何人都可以点我的教程,这将帮助我去学习,我需要知道要创建一个示例应用程序,从我的网络服务获取信息,或者是否有人愿意通过它走我,我会大大AP preciate吧!

If anyone can point me to a tutorial that will help me out to learn ALL that I need to know to create a sample app that gets information from my web service, or if anyone is willing to walk me through it, I would greatly appreciate it!

在此先感谢!

推荐答案

首先你必须做出一个HTTP连接,这样就可以从你的API响应无论是XML响应和JSON响应。您可以使用下面的code吧。 保持独立的类比活动。 : -

Initially you have to make an http connection so that you can get the response from your api be it xml response or json response. You can use the following code for it. Keep the class separate than activity. :-

public class Response {

String get_url, response;
Activity activity;

public Response(String url){
    this.get_url = url;

}

public String getResponse(){
     InputStream in = null;        
      byte[] data = new byte[1000];
        try {
              URL url = new URL(get_url);   
              URLConnection conn = url.openConnection();
              conn.connect();
            /*  conn.*/
              in = conn.getInputStream();
              Log.d("Buffer Size +++++++++++++", ""+in.toString().length());
              BufferedReader rd = new BufferedReader(new InputStreamReader(in),in.toString().length());
              String line;
              StringBuilder sb =  new StringBuilder();
              while ((line = rd.readLine()) != null) {
                    sb.append(line);
              }
              rd.close();
              response = sb.toString();

             in.read(data);
          Log.d("INPUT STREAM PROFILE RESPONSE",response);
            in.close();
        } catch (IOException e1) {
            Log.d("CONNECTION  ERROR", "+++++++++++++++++++++++++++");
            // TODO Auto-generated catch block

            e1.printStackTrace();
        }
        return response;
}
}

您可以调用这个类在这样的活动: -

You may call the class in your activity like this :-

Response res = new Response("your_url");
String getResponse = res.getResponse();

所以在这里,你得到的API的响应。

So here you get the response from the api.

现在让我们使解析器

         //Extend the class with Default Handler

         public class XMLParser extends DefaultHandler {
              //You must have basic knowledge about Array List and setter/getter methods
              // This is where the data will be stored
       ArrayList<Item> itemsList;
          Item item;
           String data;
            String type;
           private String tempVal;

                 //Create the Constructor
           public XMLParser(String data){
        itemsList = new ArrayList<Item>();

        this.data = data;

    }

     public byte parse(){

            SAXParserFactory spf = null;
            SAXParser sp = null;
            InputStream inputStream = null;

            try {
                inputStream = new ByteArrayInputStream(data.getBytes());
                spf = SAXParserFactory.newInstance();
                if (spf != null) {
                    sp = spf.newSAXParser();
                    sp.parse(inputStream, this);
                }
            }
            /*
             * Exceptions need to be handled MalformedURLException
             * ParserConfigurationException IOException SAXException
             */

            catch (Exception e) {
                System.out.println("Exception: " + e);
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null)
                        inputStream.close();
                } catch (Exception e) {
                }
            }

            if (itemsList != null && itemsList.size() > 0) {
            //  //Log.d("Array List Size",""+tipsList.get(4).getTitle());


                return 1;
            } else {
                return 0;
            }

        }

     public ArrayList<Item> getItemList(){
         return itemsList;
     }


              // Here you can check for the xml Tags
     @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {


         if(localName.equalsIgnoreCase("item")){
             item = new Item();
             Log.d("Working", "+++++++++++++++++++++++");
         }


     }
         //tempVal is the variable which stores text temporarily and you
                 // may save the data in arraylists
     public void characters(char[] ch, int start, int length)
                throws SAXException {
            tempVal = new String(ch, start, length);
        }


     @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {




         if(localName.equalsIgnoreCase("item")){
             itemsList.add(item);
             Log.d("Working in endelement", "+++++++++++++++++++++++");

            item.setTitle(tempVal);

        }
       }

结合这一切: -

现在让我们看看活动

         public void oncreate(){
               // Do something or mostly the basic code
               // Call the class to initate the connection and get the data
                 FetchList fl = new FetchList();
                  fl.execute();
                  }
          //Always better to use async task for these purposes
           public class FetchList extends asyncTask<Void,Void,Byte>{

                doinbackground{
                    // this was explained in first step
                     Response res = new Response("url");
                     String response = res.getResponse();
                     XmlParser xml = new XmlParser(response);
                      ArrayList<item> itemList = xml.getItemList();
                      xml.parse();
                 }
                }

嗯,这就是全部了。

Well that is all to it.