简体   繁体   English

将URL传递给多个非活动Java类android

[英]pass the URL to multiple non-activity java class android

In both the activity I am passing this URL http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016 ,but the thing is I am passing some dynamic parameters to the link which I passed to CustomizedListView.java activity and that same dynamic link I want to pass to XMLParser.java ,which I am unable to pass....at present I pasted the static link in the XMLParser.java but in that I want to get the link which is being passed dynamically in the CustomizedListView.java activity. 在这两个活动中,我都将传递此URL http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016 ,但事实是我正在将一些动态参数传递给我传递给CustomizedListView.java活动的链接,以及我想传递给XMLParser.java的相同动态链接,但我无法传递。...目前,我将静态链接粘贴到XMLParser.java中,但是我想要获取在CustomizedListView.java活动中动态传递的链接。

I did like this in CustomizedListView.java 我在CustomizedListView.java中做到了这一点

Intent i4 = new Intent(this, XMLParser.class);
        i4.putExtra("epuzzle", URL);
         Log.d("URLURLURLURLURL ", "> " + URL);
        startActivity(i4); 

And I did like this in XMLParser.java 我在XMLParser.java中做到了这一点

Intent intent = getIntent();
String easyPuzzle = intent.getExtras().getString("epuzzle");

CustomizedListView.java CustomizedListView.java

public class CustomizedListView extends Activity {
        // All static variables
        static final String URL = "http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016";
        // XML node keys 
        static final String KEY_SONG = "Route"; // parent node
    static final String KEY_ID = "Avail";
    static final String KEY_TITLE = "Avail";
    static final String KEY_ARTIST = "Avail";
    static final String KEY_ARTIST2 = "Avail";
    static final String KEY_DURATION = "Avail";
    static final String KEY_THUMB_URL = "thumb_url";

        ListView list;
        LazyAdapter adapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML from URL
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes <song>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key =&gt; value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);
            }

            list=(ListView)findViewById(R.id.list);

            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, songsList);
            list.setAdapter(adapter);

            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                        int position, long id) {

                }
            });
        }
    }

XMLParser.java XMLParser.java

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    /**
     * Getting XML DOM element
     * @param XML string
     * */
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

           // InputSource is = new InputSource();
             //   is.setCharacterStream(new StringReader(xml));
                doc = db.parse("http://example.com/ss.svc/APIabc?A=10&Key=XXXXX&From=19&To=221&Date=19-Apr-2016"); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

    /** Getting node value
      * @param elem element
      */
     public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }

     /**
      * Getting node value
      * @param Element node
      * @param key string
      * */
     public String getValue(Element item, String str) {
            NodeList n = item.getElementsByTagName(str);
            return this.getElementValue(n.item(0));
        }
}

Earlier my problem was Earlier Problem so I passed the static link then it worked perfectly ....and now the problem came up to make the dynamically generated link to be same in both the Java files. 较早的问题是“ 较早的问题”, 因此我通过了静态链接,然后正常工作了……。现在问题来了,使动态生成的链接在两个Java文件中都相同。

I got the solution actually I the way to do is to pass the value from the activity class to the non-activity class. 我实际上得到的解决方案是将值从活动类传递到非活动类。

So, I received the value to XMLParser.java like this below 因此,如下所示,我收到了XMLParser.java的值

     public Document getDomElement(String xml){
            Document doc = null;
<-------------LIKE THIS--------------------------->
            String var = CustomizedListView.URL;
<---------------------------------------->
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            try {

                DocumentBuilder db = dbf.newDocumentBuilder();

                //InputSource is = new InputSource();
                  //  is.setCharacterStream(new StringReader(xml));
                    doc = db.parse(var); 

Do upvote if it helped you. 如果有帮助,请进行投票。

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

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