我正在尝试从网址http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo
上的json格式中提取数据这是我的代码:
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;
}
这是logcat:
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)
它说它是一个类强制转换异常:s ..请帮助!这是android.manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pxr.tutorial.json"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestConnectionActivity"
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>
答案 0 :(得分:2)
您的TestConnectionActivity
课程需要延长Activity
:
public class TestConnectionActivity extends Activity {
.
.
.
}