Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old September 19th, 2012, 03:17 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Sep 2012
Posts: 2
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default The problem of the beginner

Hi,

Learning the basics of android I'm trying to connect the two found on the web apps. One shows a widget with two lines of text other connects to the network and retrieves the XML file.

The program compiles correctly and shows the result in the emulator (Eclipse), while on the phone (Samsung Ace - Android 2.3.6) I only have the default "Hello widget!" the declaration of variables (strings.xml).

Can someone hovers me what I'm doing wrong.
Enclosed is the code:
Code:
package org.me.zachodnik;
 
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.sax.Element;
import android.widget.RemoteViews;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.Timer;
import java.util.TimerTask;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

 
    public class Zachodnik extends AppWidgetProvider {
    //private AppWidgetManager appWidgetManager;

    	String Wsch;
    	
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
            Timer timer = new Timer();          

            String xml = getXML("http://mywwwsite.asd/rss/asd.xml");
            Document doc = XMLfromString(xml);
            NodeList nodes = doc.getElementsByTagName("result");

            
            Wsch = nodes.getLength() + "";
            timer.scheduleAtFixedRate(new ZachodyWschody(context, appWidgetManager),100, 10000);
 
        }
 
        private class ZachodyWschody extends TimerTask{
 
            RemoteViews remoteViews;
            ComponentName thisWidget;
            AppWidgetManager appWidgetManager;            
 
            public ZachodyWschody(Context context,AppWidgetManager appWidgetManager){
                this.appWidgetManager = appWidgetManager;
                remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
                thisWidget = new ComponentName(context, Zachodnik.class);                
            }
 
            @Override
            public void run(){

                CharSequence w = (CharSequence)Wsch;
                String Zach = "0000";
                CharSequence z = (CharSequence)Zach;
                 
                remoteViews.setTextViewText(R.id.widget_textview, "Wschód: " + w + "\nZachód:" +  z );
                appWidgetManager.updateAppWidget(thisWidget, remoteViews);
 
            }
        }
        
        public final static Document XMLfromString(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(is); 

            } catch (ParserConfigurationException e) {
                System.out.println("XML parse error: " + e.getMessage());
                return null;
            } catch (SAXException e) {
                System.out.println("Wrong XML file structure: " + e.getMessage());
                return null;
            } catch (IOException e) {
                System.out.println("I/O exeption: " + e.getMessage());
                return null;
            }

            return doc;

        }

         public final static String getElementValue( Node elem ) {
             Node kid;
             if( elem != null){
                 if (elem.hasChildNodes()){
                     for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                         if( kid.getNodeType() == Node.TEXT_NODE  ){
                             return kid.getNodeValue();
                         }
                     }
                 }
             }
             return "";
         }

         public static String getXML(String webURL){     
                String line = null;

                try {

                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(webURL);

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

                } catch (UnsupportedEncodingException e) {
                    line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
                } catch (MalformedURLException e) {
                    line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
                } catch (IOException e) {
                    line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
                }

                return line;

        }

        public static int numResults(Document doc){     
            Node results = doc.getDocumentElement();
            int res = -1;

            try{
                res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
            }catch(Exception e ){
                res = -1;
            }

            return res;
        }

        public static String getValue(Element item, String str) {       
            NodeList n = ((Document) item).getElementsByTagName(str);        
            return getElementValue(n.item(0));
        }
    }
AndroidManifest.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.me.zachodnik"
      android:versionCode="2"
      android:versionName="1.0" android:installLocation="internalOnly">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application  android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <!-- Broadcast Receiver that will process AppWidget updates -->
        <receiver android:name=".Zachodnik" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/zachodnik_widget_provider" />
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="9"/>
</manifest>

carriera is offline  
Reply With Quote
sponsored links
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:47 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.