Karim M. El Tel
Karim M. El Tel

Reputation: 438

XML Parsing using Java not getting any Response

am trying to get the XML file from a URL but am getting no Response and the code stops later because the String xml is null, can you tell me whats the problem ?

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

    try {


        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
                    // I printed the response here but I got nothing ! 
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
                    return xml;


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

Please be specific in you answers I appreciate your help

Upvotes: 0

Views: 671

Answers (5)

Jochem
Jochem

Reputation: 2993

You should understand Exceptions. The reason xml is null is because 'something' went wrong. This threw an Exception, probably with a good description of what went wrong. When this happens, the Exception is thrown 'up' until someone handles it.

Every subclass of Exception has a different 'flavor' and is thrown in specific cases. This enables you to 'react' on errors. For instance, you could tell the user what went wrong, or log something for debugging sake.

In your case, you 'catch' all exceptions in a single place, when an exception occurs, the code after catch (Exception e) is executed. You do nothing here but printing out some stuff (this will appear orange in your LogCat). Then you continue as if nothing happened. But xml will be null, which is bad for your program, and you apparently didn't notice the LogCat entry because your program crashes at a later point.

This time, Eldhose M Babu solved your problem. Next time, when something else goes wrong (and a lot can go wrong in htttp requests), your program will show the same behavior. Try and read up on exceptions and be careful when you handle them too silently.

Upvotes: 0

SubbaReddy PolamReddy
SubbaReddy PolamReddy

Reputation: 2113

for geturldata()

public InputStream getUrlData(String url) throws URISyntaxException,
    ClientProtocolException, IOException {

DefaultHttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(new URI(url));
HttpResponse res = client.execute(method);
return res.getEntity().getContent();

}

Upvotes: 0

SubbaReddy PolamReddy
SubbaReddy PolamReddy

Reputation: 2113

try this:

try {

    items = new ArrayList<String>();

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(new InputStreamReader(
            getUrlData(" url")));

    while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
        Log.i(TAG, "doc started");
        if (xpp.getEventType() == XmlPullParser.START_TAG) {
            if (xpp.getName().equals("entry")) {
                items.add(xpp.getAttributeValue(0));
            }
        }
        xpp.next();

    }
} catch (Throwable t) {
    Toast.makeText(this, "Request failed: " + t.toString(),
            Toast.LENGTH_LONG).show();
}

Upvotes: 0

Eldhose M Babu
Eldhose M Babu

Reputation: 14530

Why you are using HTTPPost?? You are not sending any data Even. Try with HttpGet.

Try This :

public String getXmlFromUrl(String url) throws Exception {
    return new AsyncTask<String, Void, String>() {
        @Override
        protected String doInBackground(String... params) {
            String xml = null;
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpPost = new HttpGet(params[0]);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                // I printed the response here but I got nothing !
                HttpEntity httpEntity = httpResponse.getEntity();
                xml = EntityUtils.toString(httpEntity);
                Log.i("DEMO", xml);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return xml;
        }
    }.execute(url).get();

}

Upvotes: 1

Dmytro Danylyk
Dmytro Danylyk

Reputation: 19796

Try to start your code from separate thread e.g.

 new Thread(new Runnable()
 {

    @Override
    public void run()
    {
        // TODO your code here

    }
  }).start();

Upvotes: 0

Related Questions