Solved

How to get Json object from asp.net to android

Posted on 2014-04-02
3
2,485 Views
Last Modified: 2014-04-11
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
Comment
Question by:tangteng78
  • 2
3 Comments
 
LVL 16

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

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
 

Author Comment

by:tangteng78
ID: 39977826
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
 
LVL 16

Accepted Solution

by:
Chris Harte earned 500 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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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”.…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
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.

759 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

20 Experts available now in Live!

Get 1:1 Help Now