Lavanya
Lavanya

Reputation: 621

Error while calling Web service in Android 4 and above

I used the following code for calling the web service in android.

if( requestType.equals( "GET" ) )
{
    try
    {
         // GET
        HttpGet request = new HttpGet( );       
        request.setURI( new URI( requestURL ) );        
        HttpResponse response = client.execute( request );      
        if( response == null )
        {
            Log.d( APP_NAME, "Get Response returned null" );
        }
        else
        {
            HttpEntity entity = response.getEntity( );
            String encoding = EntityUtils.getContentCharSet( entity );
            responseString = EntityUtils.toString( entity, encoding == null ? "UTF-8" : encoding );
        }
    }
    catch( Exception e )
    {
            Log.e( APP_NAME, "Error Get : " + e.getMessage( ) );
    }
}

This code is supporting upto 3.0. But not supporting in android version 4.0 and above. Please do the needful.

Upvotes: 0

Views: 173

Answers (1)

Ajay S
Ajay S

Reputation: 48612

I guess the problem is NetworkonMainThreadException.

This is because either you are performing network operation on main thread which in not allowed android version >= 3.0.

Use AsyncTask Read from http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 1

Related Questions