0

I tried an example of parsing and showing data from xml file and hence i followed parsexml tutorial. I replaced this xml file with my own xml file which i stored on my server.My xml file is valid one and i checked this by loading it on my chrome.My problem is while running my project it is throwing an error as null point error and when i debug my app near xml file it is giving an error of page not found. With this i attached my logcat. Can anyone tell me why am getting this error . I referred this but no use. My logcat is

12-26 02:34:20.230: E/Error:(1734): expected: /META read: HEAD (position:END_TAG </HEAD>@11:8 in java.io.StringReader@41829778) 
12-26 02:34:20.230: D/AndroidRuntime(1734): Shutting down VM
12-26 02:34:20.230: W/dalvikvm(1734): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
12-26 02:34:20.240: E/AndroidRuntime(1734): FATAL EXCEPTION: main
12-26 02:34:20.240: E/AndroidRuntime(1734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.mypractise/com.practise.loadimagefromxml.AndroidXMLParsingActivity}: java.lang.NullPointerException
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.os.Looper.loop(Looper.java:137)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at java.lang.reflect.Method.invoke(Method.java:525)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at dalvik.system.NativeStart.main(Native Method)
12-26 02:34:20.240: E/AndroidRuntime(1734): Caused by: java.lang.NullPointerException
12-26 02:34:20.240: E/AndroidRuntime(1734):     at com.testing.mypractise.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:55)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.Activity.performCreate(Activity.java:5133)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-26 02:34:20.240: E/AndroidRuntime(1734):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-26 02:34:20.240: E/AndroidRuntime(1734):     ... 11 more

AndroidXmlParsingActivity:

public class AndroidXMLParsingActivity extends ListActivity {

    // All static variables
    static final String URL = "http://....../.../Images/testing.xml";
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node
    static final String KEY_CATEGORY = "category";
    static final String KEY_CHANNELNAME = "channelname";
    //Intent imgvw;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tv_main);

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

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); 
        Document doc = parser.getDomElement(xml); 

        **NodeList nl = doc.getElementsByTagName(KEY_ITEM);**  => this is 55th line

        for (int i = 0; i < nl.getLength(); i++) {

            HashMap<String, String> listmap = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            listmap.put(KEY_CATEGORY, parser.getValue(e, KEY_CATEGORY));
            listmap.put(KEY_CHANNELNAME, parser.getValue(e, KEY_CHANNELNAME));

            // adding HashList to ArrayList
            channellist.add(listmap);
        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, channellist,
                R.layout.list_item,
                new String[] { KEY_CATEGORY, KEY_CHANNELNAME}, new int[] {
                        R.id.category, R.id.cahnnel_name});

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String category = ((TextView) view.findViewById(R.id.category)).getText().toString();
                String channelname = ((TextView) view.findViewById(R.id.cahnnel_name)).getText().toString();

                try {
                    ImageView imgvw = (ImageView) findViewById (R.id.image_logo);
                      Bitmap bitmap;
                    bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://192.168.2.57:8080/Iptvtest/Images/bnews1.png").getContent());
                    imgvw.setImageBitmap(bitmap);
                } 
                catch (MalformedURLException e) 
                {

                    e.printStackTrace();
                } 
                catch (IOException e) 
                {

                    e.printStackTrace();
                }



                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_CATEGORY, category);
                in.putExtra(KEY_CHANNELNAME, channelname);
                //in.putExtras(imgvw);

                startActivity(in);

            }
        });
    }
}

And am facing this error in getDomElement on xmlparser class:

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));  // error here.
                doc = db.parse(is); 

            } 
        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));
        }
}

and the error is

HTTP Error 405 - The HTTP verb used to access this page is not allowed.<br>Internet Information Services (IIS)
8
  • 2
    What you have at line 55 in AndroidXMLParsingActivity.java. Post your code or check line 55 in AndroidXMLParsingActivity.java. It produce null pointer exception Commented Dec 26, 2013 at 9:02
  • yes sathishkumar is right. Commented Dec 26, 2013 at 9:04
  • 1
    seems like doc is null, it might not be related to your problem but it is VERY important - you should not perform IO operation on the UI thread (e.g. in onCreate), if you need to download content from the web, do it using an AsyncTask or a similar approach. Commented Dec 26, 2013 at 9:08
  • When am running the example code it is working perfect but when running my own xml it is throwing error Commented Dec 26, 2013 at 9:10
  • Post your AndroidXMLParsingActivity() line 55 from there u are getting this issue. Commented Dec 26, 2013 at 11:30

1 Answer 1

1

Finally i Resolved my null pointer exception error and meta read error. Instead of HTTPPost i declared HTTPGET. This resolved my error. The reason for this is

The HTTP application hosting that content is expecting something else - most likely GET. Apache HTTP Client has a class for that - HTTPGet... naturally. As for the difference between the two. Well the main difference is that the GET method encodes paramaters as part of the URL (for example you may notice ?apiAccessCode=api&parameter2=something), whereas POST puts the content as part of the HTTP message body. HTTP GET is limited as well to 256 characters. Also the use cases are meant to be different. HTTP GET should be used for data retrieval only, and HTTP Post can be used for updating content.

and my code is

XMLParser:
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet (url);

            HttpResponse httpResponse = httpClient.execute(httpget); // here i changed my httppost to httpget.
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } 

Hope this may help some one :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.