How to get Json object from asp.net to android

Posted on 2014-04-02
Medium Priority
Last Modified: 2014-04-11
I have a JSON object at this page http://www.toypanic.com/webservice/ws.aspx

Now how do I code it from my android part to retrieve it? Do i need a separate library like ksoap?

Any advise is appreciated.
Question by:tangteng78
  • 2
LVL 17

Expert Comment

by:Chris Harte
ID: 39973085
You do not need anything external, android provides all the libraries you need to develop a json parser


This is not a bad online tutorial

Author Comment

ID: 39977826
Alright, this is my code in android.

            TextView myListView = (TextView)findViewById(R.id.textView1);
            String result = "";
            //lets try to connect
                  //create a new client object
                  HttpClient httpclient = new DefaultHttpClient();
                  //HttpGet httppost = new HttpGet("http://www.toypanic.com/webservice/ws.aspx");
                  HttpPost httppost = new HttpPost("http://www.toypanic.com/webservice/ws.aspx");
                  //execute the post and get the response object
                  HttpResponse response = httpclient.execute(httppost);
                  //get the message from the response
                  HttpEntity entity = response.getEntity();
                  //get the content of the message
                  InputStream webs = entity.getContent();
                  //convert response to string
                        BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while((line=reader.readLine()) != null)
                              sb.append(line + "\n");
                        //close our input stream
                        //convert our stringbuilder object to a string!
                  catch(Exception e)
                        Log.e("log_tag", "Error converting result " +e.toString());
            catch(Exception e)
                  Log.e("log_tag", "Error in http connection " + e.toString());

I'm getting the java.net.unknownhostexception (see the attached). As i know this is due to the internet permission, and I have set the in the manifest.xml as below.

I'm at wit's end. Any help or pointer is greatly appreciated.

PS: I run it directly on my android phone.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >

        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

LVL 17

Accepted Solution

Chris Harte earned 2000 total points
ID: 39984568
You cannot access the internet from your main ui thread, onCreate() in this case, you have to use a separate thread or AsyncTask


A lot of extra work but it stops a misbehaving app from tying up the device.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

If you use the Google Now Launcher, as an aftermarket add on, have a Samsung Note 5 and are worried about power consumption be wary of using the ultra power saving mode.  Here is what happened to me when I made the mistake of trying this out...
Custom Android App Development is the need of an hour for several businesses. If you go through the right firm for Android Application Development, it can make a huge difference to the ways in which customers interact with your business.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

597 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