Solved

Trying to get XML from an URL

Posted on 2014-04-02
1
1,668 Views
Last Modified: 2014-04-18
Previously, I used the the XML file from the assets folder. The app can read it very well. The next step I want put this XML on the web server. But at this stage, the app cannot recognize any data. It confused me couple of days.

AssetManager asset = getAssets();
InputStream input = asset.open("student.xml");
List<Student> list = ParserByPULL.getStudents(input);

Open in new window


Everything works fine if the file in assets folder. Then I tried to get it from an URL.

String path = "http://fthgyj.tup632.cnaaa11.com/student.xml";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
InputStream input = url.openConnection().getInputStream(); 
List<Student> list = ParserByPULL.getStudents(input);

Open in new window


I have added the permission of connecting the INTERNET at manifest file.
After these steps, I have run the app. Unfortunately, nothing shows on the screen. There are some permission warnings in LogCat, but it shows all time when I fetch the file from Assets. So I dont think the problem is the permission.  
Does anyone has an idea about this? Thank you.
0
Comment
Question by:ron154
1 Comment
 
LVL 12

Accepted Solution

by:
KRUNAL TAILOR earned 500 total points
ID: 39974048
Hi ron154,

First let me know what are you trying to achieve?

If you wan't to send xml file on server(means upload on server) in that case this below links will help you,
1) http://androidcodeforbeginners.blogspot.in/2013/05/send-xml-data-from-android-to-php-server.html
2) http://beshkin.lv/post-xml-file-to-a-page-from-android/

But,

if you wan't to get xml file from server (means download from server), because your code suggest it you are trying to download it from server. may be i am wrong but, if you wan't to grab the xml file from server in that case you can do this.

uri = new HttpGet("http://fthgyj.tup632.cnaaa11.com/student.xml");
 
client = new DefaultHttpClient();
resp = client.execute(uri);
status = resp.getStatusLine();
if (status.getStatusCode() == "200") {
    factory = DocumentBuilderFactory.newInstance();
    builder = factory.newDocumentBuilder();
    doc = builder.parse(resp.getEntity().getContent());
    ...
    // Your code for reading XML doc 
}

Open in new window


OR

1) By using HTTP Request getting the XML content.
2) Parsing the XML content and retrieve DOM element in the XML.
3) Retrieve each element child element value by using node name of element.

Follow this link: http://androidituts.com/android-xml-parsing-tutorial/

Thanks & Regards,
Krunal T. Tailor
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Google Drive" not show in "My Files" on Samsung Galaxy Tab A 12 88
iPhone v Android phone 7 34
iPad Testing Issue 4 27
Built in memory vs SD card. 3 36
Introduction Everyone is using a smartphone these days. Contacts, messages, applications, documents, bookmarks, pictures, videos and other stuff which you created and are present only on your device come under the umbrella called “Personal Data”.…
A short article about problems I had with the new location API and permissions in Marshmallow
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now