从网站中提取JSON [英] extracting json from a website

查看:167
本文介绍了从网站中提取JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从该URL <一个JSON格式提取数据href=\"http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo\" rel=\"nofollow\">http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo

这是我的code:

 包com.pxr.tutorial.json;进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.HashMap;进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.util.Log;公共类TestConnectionActivity {    公共静态的JSONObject getJSONfromURL(字符串URL){
        InputStream为= NULL;
        字符串结果=;
        JSONObject的jArray = NULL;        // HTTP POST
        尝试{
                HttpClient的HttpClient的=新DefaultHttpClient();
                HTTPGET httppost =新HTTPGET(URL);
                HTT presponse响应= httpclient.execute(httppost);
                HttpEntity实体= response.getEntity();
                是= entity.getContent();        }赶上(例外五){
                Log.e(log_tag,在HTTP连接错误+ e.toString());
        }      //响应转换为字符串
        尝试{
                读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-1),8);
                StringBuilder的SB =新的StringBuilder();
                串线= NULL;
                而((行= reader.readLine())!= NULL){
                        sb.append(行+\\ n);
                }
                is.close();
                结果= sb.toString();
        }赶上(例外五){
                Log.e(log_tag,错误转换结果+ e.toString());
        }        尝试{            jArray =新的JSONObject(结果);
        }赶上(JSONException E){
                Log.e(log_tag,错误分析数据+ e.toString());
        }        返回jArray;
    }

这是logcat的:

  17 10-20:44:20.149:ERROR / AndroidRuntime(449):了java.lang.RuntimeException:无法实例活动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):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.os.Handler.dispatchMessage(Handler.java:99)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.os.Looper.loop(Looper.java:123)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.app.ActivityThread.main(ActivityThread.java:3683)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在java.lang.reflect.Method.invokeNative(本机方法)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在java.lang.reflect.Method.invoke(Method.java:507)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在dalvik.system.NativeStart.main(本机方法)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):java.lang.ClassCastException:产生的原因com.pxr.tutorial.json.TestConnectionActivity
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-20 17:44:20.149:ERROR / AndroidRuntime(449):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)

它说,它的一类转换异常:秒。请帮助!这里的android.manifest

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.pxr.tutorial.json
      安卓版code =1
      机器人:=的versionName1.0&GT;
    &LT;采用-SDK安卓的minSdkVersion =10/&GT;
    &LT;使用许可权的android:NAME =android.permission.INTERNET对&GT;&LT; /使用许可权&GT;    &lt;应用机器人:图标=@绘制/图标机器人:标签=@字符串/ APP_NAME&GT;
        &LT;活动机器人:名字=。TestConnectionActivity
                  机器人:标签=@字符串/ APP_NAME&GT;
            &所述;意图滤光器&gt;
                &lt;作用机器人:名字=android.intent.action.MAIN/&GT;
                &LT;类机器人:名字=android.intent.category.LAUNCHER/&GT;
            &所述; /意图滤光器&gt;
        &LT; /活性GT;    &LT; /用途&gt;
&LT; /清单&GT;


解决方案

TestConnectionActivity 类需要扩展活动

 公共类TestConnectionActivity延伸活动{



}

I am trying to extract data from the json format on the url http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo

This is my 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;
    }

This is the 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)

It says its a class cast exception :s .. Kindly Help! here's the 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>

解决方案

Your TestConnectionActivity class needs to extend Activity:

public class TestConnectionActivity extends Activity {
.
.
.
}

这篇关于从网站中提取JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆