Note that there are some explanatory texts on larger screens.

plurals
  1. POextracting json from a website
    primarykey
    data
    text
    <p>I am trying to extract data from the json format on the url <a href="http://api.geonames.org/earthquakesJSON?north=44.1&amp;south=-9.9&amp;east=-22.4&amp;west=55.2&amp;username=demo" rel="nofollow">http://api.geonames.org/earthquakesJSON?north=44.1&amp;south=-9.9&amp;east=-22.4&amp;west=55.2&amp;username=demo</a> </p> <p>This is my code: </p> <pre><code>package com.pxr.tutorial.json; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class TestConnectionActivity { public static JSONObject getJSONfromURL(String url){ InputStream is = null; String result = ""; JSONObject jArray = null; //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpGet httppost = new HttpGet(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } try{ jArray = new JSONObject(result); }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } return jArray; } </code></pre> <p>This is the logcat:</p> <pre><code>10-20 17:44:20.149: ERROR/AndroidRuntime(449): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pxr.tutorial.json/com.pxr.tutorial.json.TestConnectionActivity}: java.lang.ClassCastException: com.pxr.tutorial.json.TestConnectionActivity 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.os.Handler.dispatchMessage(Handler.java:99) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.os.Looper.loop(Looper.java:123) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.ActivityThread.main(ActivityThread.java:3683) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at java.lang.reflect.Method.invokeNative(Native Method) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at java.lang.reflect.Method.invoke(Method.java:507) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at dalvik.system.NativeStart.main(Native Method) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): Caused by: java.lang.ClassCastException: com.pxr.tutorial.json.TestConnectionActivity 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 10-20 17:44:20.149: ERROR/AndroidRuntime(449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) </code></pre> <p>It says its a class cast exception :s .. Kindly Help! here's the android.manifest</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pxr.tutorial.json" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".TestConnectionActivity" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload