2012-03-16 15 views
8

मैं एक NetworkOnMainThreadException मिलता है जब मैं निम्नलिखित कोड लागू करने के लिए प्रयास करें:NetworkOnMainThread

public class HandlingXMLStuff extends ListActivity{ 
static final String URL = "xml_file"; 

static final String ITEM = "item"; //parent 
    static final String Id = "id"; 
    static final String Name = "name"; 
    static final String Desc = "desc"; 
    static final String Link = "Link"; 

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

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

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


NodeList nl = doc.getElementsByTagName(ITEM); 


//START: loop through all item nodes <item> 
for (int i = 0;i<nl.getLength();i++){ 
    //lets create our HASHMAP!! (feeds items into our ArrayList) 
    HashMap<String, String> map = new HashMap<String, String>(); 
    Element e = (Element) nl.item(i); 
    //add each child node to the HashMap (key, value/<String, String>) 
    map.put(Name, parser.getValue(e, Name)); 
    map.put(Desc, parser.getValue(e, Desc)); 
    map.put(Link, parser.getValue(e, Link)); 


    menuItems.add(map); 

}//DONE 


ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item, 
     new String[] {Name, Desc, Link}, new int []{R.id.name, R.id.description, R.id.link}); 

setListAdapter(adapter); 
     } 
} 

और हैंडलर:

public class xmlparser{ 

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

try { 

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

public String getValue(Element item, String str) {  
     NodeList n = item.getElementsByTagName(str);   
     return this.getElementValue(n.item(0)); 
    } 

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

}

किसी भी विचार क्यों? इसे काम करना चाहिए, मैंने पढ़े गए सभी ट्यूटोरियल इसे वर्किंग कोड के रूप में देखते हैं लेकिन यह नहीं चलता है और केवल अपवाद फेंकता है। मैंने पढ़ा है कि मुझे एसिन्टास्क को लागू करने की आवश्यकता हो सकती है लेकिन मैं इसके लिए नया हूं और यह सुनिश्चित नहीं करता कि किन हिस्सों को अपने स्वयं के धागे की आवश्यकता है। किसी भी मदद के लिए धन्यवाद, आलोचना (रचनात्मक), सुझाव, आदि

+0

की [android.os.NetworkOnMainThreadException] (http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) –

उत्तर

10

कोई विचार क्यों?

क्योंकि, यदि कोड के उस हिस्से को मुख्य अनुप्रयोग धागे पर निष्पादित किया जा रहा है, तो आप मुख्य अनुप्रयोग धागे पर नेटवर्क I/O कर रहे हैं।

मैंने पढ़ा है कि मुझे एसिंक्टास्क को लागू करने की आवश्यकता हो सकती है लेकिन मैं इसके लिए नया हूं और यह सुनिश्चित नहीं करता कि किन हिस्सों को अपने स्वयं के धागे की आवश्यकता है।

मैं नेटवर्क मैं/हे और doInBackground() में पार्स करने और एक AsyncTask की onPostExecute() में setListAdapter() कॉल जाते थे।

+0

धन्यवाद कि बहुत मदद की संभावित डुप्लिकेट, यह अब काम कर रहा है :) – Bob

+0

और रुचि किसी को भी यहाँ है के लिए कार्य कोड: – Bob

+1

कोड कहां है? – auraham

1

CommonsWare उत्तर में जोड़कर, NetworkOnMainThreadException को 2.3.3 (जिंजरब्रेड_MR1) और 3.0 (हनीकॉम) के बीच कभी जोड़ा गया था।

/** 
    * For apps targetting SDK Honeycomb or later, we don't allow 
    * network usage on the main event loop/UI thread. 
    * 
    * Note to those grepping: this is what ultimately throws 
    * NetworkOnMainThreadException ... 
    */ 
    if (data.appInfo.targetSdkVersion > 9) { 
     StrictMode.enableDeathOnNetwork(); 
    } 

मुझे लगता है कि ट्यूटोरियल है कि आप निम्न कर रहे थे इस से पहले लिखा गया था जगह में डाल दिया गया था, और इसलिए का कारण नहीं: आप

android.app.ActivityThread 

को देखें, तो आप कोड का निम्न भाग मिलेगा NetworkOnMainThreadException। AsyncTask के बारे में CommonsWare निर्देशों का पालन करें और आप अपनी त्रुटि ठीक कर देंगे।

+0

धन्यवाद कि जानना अच्छा है :) – Bob

3

आप बस अपने कोड का परीक्षण करने के लिए, और अभी तक किसी भी अधिक जटिलताओं को जोड़ने के लिए नहीं करना चाहती चाहते हैं, तो आप अपने onCreate()

if (android.os.Build.VERSION.SDK_INT > 9) { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
} 

में जोड़ना, आप इस स्थायी होने के लिए नहीं करना चाहते हैं कर सकते हैं के रूप में यूआई थ्रेड पर नेटवर्क ऑपरेशंस ऐप का उपयोग करते समय एक बुरा अनुभव बनाता है, लेकिन परीक्षण करते समय उपयोगी हो सकता है।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^