0

Im using the Google Directions API to get the time between to locations and Im trying to get the data from a URL into a JSON object so that I can parse it for the data I need. The URL is:

http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false

I go to this URL using an intent.

String url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false"
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

This brings up json data in the browser and I would like to put it into a json object so that I can get whatever data I need using:

JSONObject durationObject = null; 
durationObject = jsonObject.getJSONObject("duration");

I have looked this up online but everything I try ends up crashing or not working. Below is some code I have tried but does not work. Nothing ends up being written to String result because the if statement executes.

InputStream is = null;
String result = "";
JSONObject jsonObject = null;
try {   
    HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
    HttpPost httppost = new HttpPost(url);
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();

    // Read response to string
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),1024);
    StringBuilder sb = new StringBuilder();
    String line = "";
    while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
    }
    is.close();
    result = sb.toString(); 


    if (result.isEmpty()) { 
        result = "nothing"; 
        Toast msg = Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG);
    msg.show();
    }
 // Convert string to object

    jsonObject = new JSONObject(result);

This is the logcat output:

04-16 18:38:42.965: W/dalvikvm(9331): threadid=1: thread exiting with uncaught exception (group=0x40c371f8)
04-16 18:38:42.975: E/AndroidRuntime(9331): FATAL EXCEPTION: main
04-16 18:38:42.975: E/AndroidRuntime(9331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.os.NetworkOnMainThreadException
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.os.Looper.loop(Looper.java:137)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.ActivityThread.main(ActivityThread.java:4507)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at java.lang.reflect.Method.invoke(Method.java:511)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at dalvik.system.NativeStart.main(Native Method)
04-16 18:38:42.975: E/AndroidRuntime(9331): Caused by: android.os.NetworkOnMainThreadException
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at com.example.test.MainActivity.onCreate(MainActivity.java:93)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.Activity.performCreate(Activity.java:4465)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
04-16 18:38:42.975: E/AndroidRuntime(9331):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
04-16 18:38:42.975: E/AndroidRuntime(9331):     ... 11 more

I was looking into it and it seems it has something to do with the strict mode being enabled. Any help on how to fix this problem would be great.

homes
  • 265
  • 1
  • 3
  • 12

2 Answers2

0

This is a working code that I am using

    private DefaultHttpClient httpclient = new DefaultHttpClient();

    public JSONObject getObjectInfo(String requestURL) throws Exception {

    JSONObject responseObject = null;
    Log.d(getClass().getSimpleName(), "HttpClient: getObjectInfo: "
            + requestURL);
    HttpGet httpget = new HttpGet(requestURL);
    HttpResponse response;
    try {
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        Log.d(getClass().getSimpleName(), "HttpClient: getObjectInfo: "
                + "respondreceived");
        if (entity != null) {
            InputStream instream = entity.getContent();
            Log.d("httpclient",
                    "HttpClient: getObjectInfo: converting to string");
            String jsonObjectString = Conversion
                    .convertStreamToString(instream);

            try {
                responseObject = new JSONObject(jsonObjectString);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalStateException e1) {
        e1.printStackTrace();
    } finally {

    }
    return responseObject;
}

Method for converting the stream to string:

    public static String convertStreamToString(java.io.InputStream is) {

    try {
        return new java.util.Scanner(is).useDelimiter("\\A").next();
    } catch (java.util.NoSuchElementException e) {
        return "";
    }
}

Saying that, I 'd suggest using Jakson library to create a class from the received json object. here is an example of how to use Jakson library.

Saeid Farivar
  • 1,667
  • 24
  • 43
  • I've tried your code as well but the same thing happens. The app crashes and I think it is because of the strict mode blocking the main thread. – homes Apr 16 '13 at 23:46
  • Try to comment the "intent code",hard code the url of google api and check whether the url gets data or not. Also check, you have taken internet permission in the manifest.ini or not. – Sarim Javaid Khan Apr 17 '13 at 00:45
  • @homes check out [this] (http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) for network stuff on main thread. – Saeid Farivar Apr 18 '13 at 15:31
0

What is happening here is common, you are trying to perform some long running operation on the main thread of your application.

What that means, is that the main thread is in charge of updating the UI, to enforce a smooth experience, if android detects you are doing some long running operation that is going to be blocking, it will force shutdown the app, to let you know that this is bad.

There are many alternative solution, such as AsynTask, or IntentService.

IntentService is a service that runs when you sent a intent to it:

public class NetworkHandler extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
         //Do what Saeid Farivar is saying here
    }
}

To activate the IntentService, simply do:

Intent intent = new Intent(mContext, NetworkHandler.class);
startService(intent);
wangyif2
  • 2,843
  • 2
  • 24
  • 29
  • thanks for explaining the problem and providing alternative solutions suggestions. I used asynctask and implemented the querying/parsing of the json in the doInBackground function and it worked fine. – homes Apr 20 '13 at 04:59