Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8478681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:48:43+00:00 2026-06-10T18:48:43+00:00

I have created an app which retrieves the data from a url using xml

  • 0

I have created an app which retrieves the data from a url using xml pull parser.It is working fine on emulator but when i test it in tab it is showing networkonmainthread exception.My code is:

package com.example.androidsample4;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class UNIXclass extends Activity 
{
    String unixcommands[];

    AutoCompleteTextView actv1;

    protected URL params;
    static final String URL="http://cympac.com/apps/xmlfile.xml";
    static final String KEY_COMMAND = "command"; // parent node
    static final String KEY_WORD = "word";
    static final String KEY_EXPLANATION = "explanation";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv1=(TextView)findViewById(R.id.textView1);
        AutoCompleteTextView actv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        Button b1=(Button)findViewById(R.id.button1);
        unixcommands=getResources().getStringArray(R.array.Commands);
        ArrayAdapter<String> adapter =new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,unixcommands);
        actv1 = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        actv1.setThreshold(1);
        actv1.setAdapter(adapter);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) 
            {
                Mytask task=new Mytask();
                task.doInBackground(params);


            }
        });
    }
    private class Mytask extends AsyncTask<java.net.URL, Void, Void>
    {

        @Override
        protected Void doInBackground(java.net.URL... params) 
        {
            TextView myXmlContent = (TextView)findViewById(R.id.textView2);
            String stringXmlContent = null;
            try {
                stringXmlContent = getEventsFromAnXML();   //   54 line
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getBaseContext(), "error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getBaseContext(), "error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
            }
            myXmlContent.setText(stringXmlContent);
            return null;

        }

    }
    public String getEventsFromAnXML() throws XmlPullParserException,IOException
    {
        StringBuffer stringBuffer=new StringBuffer();

        java.net.URL url=new java.net.URL("http://cympac.com/apps/xmlfile.xml");
        XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp=factory.newPullParser();
        //xpp.setInput(getInputStream(url),"UTF-8");         //84 line
        xpp.setInput(url.openConnection().getInputStream(), "UTF-8");
        int eventType = xpp.getEventType();
        String word = null;

        String tag;
        while ((eventType = xpp.next()) != XmlPullParser.END_DOCUMENT)
        {
            if(XmlPullParser.START_TAG==eventType)
            {
                tag=xpp.getName();
                //stringBuffer.append("\n"+tag);
                if (tag.equals("word"))
                {
                    eventType = xpp.next();
                    word = xpp.getText();
                }
                else if (tag.equals("explanation"))
                {
                    eventType = xpp.next();
                   //if ("cancel".equals(word))
                    if(actv1.getText().toString().equals(word))

                    {
                        stringBuffer.append("\n" + xpp.getText());
                    }
                }

            }

        }
        return stringBuffer.toString();



    }



}

I read that when we have to use network then we shuld use Asynctask.So i used it,But still it is showing the same error.
Please help me to solve this.
logcat o/p

08-21 16:36:41.087 E/AndroidRuntime( 4076): FATAL EXCEPTION: main
08-21 16:36:41.087 E/AndroidRuntime( 4076): android.os.NetworkOnMainThreadException
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at java.net.InetAddress.lookupHostByName(InetAddress.java:477)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:277)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at java.net.InetAddress.getAllByName(InetAddress.java:249)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:304)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:292)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:274)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1038)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at com.example.androidsample4.SANclass.getEventsFromAnXML(SANclass.java:115)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at com.example.androidsample4.SANclass$Mytask.doInBackground(SANclass.java:92)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at com.example.androidsample4.SANclass$1.onClick(SANclass.java:58)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.view.View.performClick(View.java:3131)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.view.View$PerformClick.run(View.java:12035)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.os.Handler.handleCallback(Handler.java:587)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.os.Looper.loop(Looper.java:132)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at java.lang.reflect.Method.invoke(Method.java:491)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-21 16:36:41.087 E/AndroidRuntime( 4076): at dalvik.system.NativeStart.main(Native Method)
08-21 16:36:41.092 V/AudioPolicyManager( 2713): releaseOutput() 1
08-21 16:36:41.092 W/ActivityManager( 2891):   Force finishing activity com.example.androidsample4/.SANclass

I am getting force close if i include “new Mytask.execute()” method.What could be the reason for this?Please guide me…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:48:45+00:00Added an answer on June 10, 2026 at 6:48 pm

    Dont update the UI in back ground thread.It should be done in onPostExecute()

    Toast.makeText(getBaseContext(), "error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
    myXmlContent.setText(stringXmlContent);
    

    Do this steps in onPostExecute()

    Read my previous post

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a FB App which I created using Fandjango. It works fine if
I have created an animated game app, In which I am using NSTimer to
I have created an app which allows users to buy non-consumable content. The retrieving-ids-payment-process
I have created an app which allows users to buy non-consumable content. The retrieving-ids-payment-process
I have created an app in which I have used achartengine to construct the
I've created an app which contains form and that have to filled up in
I have created an app that uses NSTimer, which gets triggered each second. My
I have created a library which reads the app.config file and gets the type
I have some XML data retrieved from a web service that I use to
My app has a background thread that periodically retrieves data from an external source,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.