Solved

How to get Json object from asp.net to android

Posted on 2014-04-02
3
2,515 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

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
Migrating From Parse To AWS 8 155
Manifest Android File Permission 1 49
Need Help To Develop Android App 1 38
Advice on suitable tablet device. 13 89
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
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.

910 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

24 Experts available now in Live!

Get 1:1 Help Now