Solved

How to get Json object from asp.net to android

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

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 17

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
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…

730 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