• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2898
  • Last Modified:

How to get Json object from asp.net to android

Hi,
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.
0
tangteng78
Asked:
tangteng78
  • 2
1 Solution
 
Chris HarteThaumaturgeCommented:
You do not need anything external, android provides all the libraries you need to develop a json parser

http://developer.android.com/reference/org/json/package-summary.html

This is not a bad online tutorial
http://www.vogella.com/tutorials/AndroidJSON/article.html
0
 
tangteng78Author Commented:
Alright, this is my code in android.

super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            TextView myListView = (TextView)findViewById(R.id.textView1);
            String result = "";
            
            //lets try to connect
            try{
                  
                        
                  //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
                  
                  try{
                        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
                        webs.close();
                        //convert our stringbuilder object to a string!
                        result=sb.toString();
                        
                  }
                  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"
    package="com.example.jsonapps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        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"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.jsonapps.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
json-error.jpg
0
 
Chris HarteThaumaturgeCommented:
You cannot access the internet from your main ui thread, onCreate() in this case, you have to use a separate thread or AsyncTask

http://developer.android.com/training/basics/network-ops/connecting.html

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now