I cant call url xml parser

220 Views Asked by At

my app get forclose, cant running with url have more caracter. im use xml parser for article in my app. if i call url like this ,my app force close.

   http://api.news.com/newsServices.asmx/getArticleByCategoryWithDetails?categoryname=nasional&start=0&limit=10

but if i call url like this, my app can running.

  http://api.news.com/news,Services.asmx/getArticle

this is my code :

   public class news extends Activity {

    private Activity activity;
    public ImageLoader imageLoader;

String URL = "http://api.news.com/newsServices.asmx/getArticleByCategoryWithDetails?categoryname=nasional&start=0&limit=10";
// XML node key

static final String KEY_ARTICLE = "Article"; // parent node
static final String KEY_ID = "ArticleID";
static final String KEY_AUTHOR = "Author";
static final String KEY_HEADLINE = "Headline";
static final String KEY_ARTICLEDATE = "ArticleDate";
static final String KEY_VIEWED = "Viewed";
static final String KEY_PUBLISHEDAT = "PublishedAt";
static final String KEY_SUMMARY = "Summary";
static final String KEY_BODY = "Body";
static final String KEY_IMAGEURL = "ImageURL";
static final String KEY_SLUG = "Slug";
static final String KEY_CATEGORY = "Category";


ListView list ;
LazyAdapter adapter;
private NodeList nl;
private xmlParser parser;

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

    activity = this;      

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

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

    Log.i("parser star", xml);

    nl = doc.getElementsByTagName(KEY_ARTICLE);
    // looping through all song nodes <song>
    for (int i = 1; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_HEADLINE, parser.getValue(e, KEY_HEADLINE));
        map.put(KEY_ARTICLEDATE, parser.getValue(e, KEY_ARTICLEDATE));
        map.put(KEY_IMAGEURL, parser.getValue(e, KEY_IMAGEURL));
        map.put(KEY_BODY, parser.getValue(e,KEY_BODY));

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

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


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

logcat :

       12-19 09:40:06.154: E/Error:(30404): Unexpected token (position:TEXT Request format i...@2:1 in java.io.StringReader@413cc940) 
       12-19 09:40:06.154: I/parser star(30404): Request format is invalid: .
       12-19 09:40:06.154: D/AndroidRuntime(30404): Shutting down VM
       12-19 09:40:06.154: W/dalvikvm(30404): threadid=1: thread exiting with uncaught exception (group=0x40a991f8)
       12-19 09:40:06.154: E/AndroidRuntime(30404): FATAL EXCEPTION: main
       12-19 09:40:06.154: E/AndroidRuntime(30404): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.news/com.news.news}: java.lang.NullPointerException
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.os.Handler.dispatchMessage(Handler.java:99)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.os.Looper.loop(Looper.java:137)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.ActivityThread.main(ActivityThread.java:4424)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at java.lang.reflect.Method.invokeNative(Native Method)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at java.lang.reflect.Method.invoke(Method.java:511)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at dalvik.system.NativeStart.main(Native Method)
       12-19 09:40:06.154: E/AndroidRuntime(30404): Caused by: java.lang.NullPointerException
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at com.news.news.onCreate(news.java:80)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.Activity.performCreate(Activity.java:4465)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
       12-19 09:40:06.154: E/AndroidRuntime(30404):     ... 11 more

this is xml parser class

        public class xmlParser {

// constructor
public xmlParser() {

}

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

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


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

There are 0 best solutions below