Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:20:03+00:00 2026-06-15T07:20:03+00:00

In my application I use JSON webservice to get data from Google Navigation api

  • 0

In my application I use JSON webservice to get data from Google Navigation api. I use the code is below.
I got the following exception

android.os.NetworkOnMainThreadException.

How to use AsyncTask? here is my code. Thanks.`public class MainActivity extends MapActivity {

  MapView mapView ;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    System.out.println("*************1**************1");
    setContentView(R.layout.activity_main);
    System.out.println("*************2**************");
    mapView = (MapView) findViewById(R.id.mapv); 
    System.out.println("*************3**************");



                Route route = directions(new GeoPoint((int)(26.2*1E6),(int)(50.6*1E6)), new GeoPoint((int)(26.3*1E6),(int)(50.7*1E6)));

              RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
              mapView.getOverlays().add(routeOverlay);
            mapView.invalidate();


    System.out.println("*************4**************");

}



@SuppressLint("ParserError")
private Route directions(final GeoPoint start, final GeoPoint dest) {



    //https://developers.google.com/maps/documentation/directions/#JSON <- get api
    String jsonURL = "http://maps.googleapis.com/maps/api/directions/json?";
    final StringBuffer sBuf = new StringBuffer(jsonURL);
    sBuf.append("origin=");
    sBuf.append(start.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(start.getLongitudeE6()/1E6);
    sBuf.append("&destination=");
    sBuf.append(dest.getLatitudeE6()/1E6);
    sBuf.append(',');
    sBuf.append(dest.getLongitudeE6()/1E6);
    sBuf.append("&sensor=true&mode=driving");

    Parser parser = new GoogleParser(sBuf.toString());
      Route   r =  parser.parse();
   System.out.println("********r in thread*****" +r);                  
            return r;
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}
`

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:20:04+00:00Added an answer on June 15, 2026 at 7:20 am

    @imran khan idea of using another thread is the right one. From HoneyComb and above, you can’t do any networking operation inside the UI Thread. The UI Thread is the thread responsible for displaying views and receiving touch events and it must be free for this, not blocked by a long running operation. (Otherwise you got an ANR on Android).

    But it’s actually not a very good idea to use an AsyncTask for long running operations like a network Request. The AsyncTask javadocs even states they have been designed for short runnning operations…

    The best approach is to use a service for that. I encourage you to read this infographics from the RoboSpice library and use RoboSpice, it will ease creating a service with networking and will make it easier for your to parse your json content using Spring Android.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use json-rpc client from json-rpc.net in my windows phone application. Code: var client
Ok i need to use json on my iOS application to send/receive data to/from
(sorry for the english) I have a ASP .net webservice that get data from
I need to get Json data from a C# web service. I know there
I'm trying to write an iOS application that'll get data from a web server
Can someone explain how to use JSON-RPC.net in my application for windows phone. When
Somebody said that when your PHP code and application use global variables then it
For an iPhone application I need to request data from an online database. I
In one of my current applications, I need to get customer data from a
I am busy working on a mobile application that retrieves data from a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.