无法在Android中解析来自天气api的Json数据

无法在Android中解析来自天气api的Json数据,android,json,Android,Json,我试图在Android中解析来自天气api的Json数据。 但是我有错误。以下是logcat错误列表: 10-17 21:22:36.395: E/log_tag(304): Error in http connection java.net.UnknownHostException: api.openweathermap.org 10-17 21:22:36.395: E/log_tag(304): Error converting result java.lang.NullPointerEx

我试图在Android中解析来自天气api的Json数据。 但是我有错误。以下是logcat错误列表:

10-17 21:22:36.395: E/log_tag(304): Error in http connection java.net.UnknownHostException: api.openweathermap.org
10-17 21:22:36.395: E/log_tag(304): Error converting result java.lang.NullPointerException
10-17 21:22:36.425: E/log_tag(304): Error parsing data org.json.JSONException: End of input at character 0 of 
10-17 21:22:36.447: E/AndroidRuntime(304): FATAL EXCEPTION: AsyncTask #1
10-17 21:22:36.447: E/AndroidRuntime(304): java.lang.RuntimeException: An error occured while executing doInBackground()
10-17 21:22:36.447: E/AndroidRuntime(304):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.lang.Thread.run(Thread.java:1096)
10-17 21:22:36.447: E/AndroidRuntime(304): Caused by: java.lang.NullPointerException
10-17 21:22:36.447: E/AndroidRuntime(304):  at com.example.work.MainActivity$DownloadJSON.doInBackground(MainActivity.java:63)
10-17 21:22:36.447: E/AndroidRuntime(304):  at com.example.work.MainActivity$DownloadJSON.doInBackground(MainActivity.java:1)
10-17 21:22:36.447: E/AndroidRuntime(304):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-17 21:22:36.447: E/AndroidRuntime(304):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-17 21:22:36.447: E/AndroidRuntime(304):  ... 4 more
这是我的密码: MainActivity.java:

package com.example.work;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class MainActivity extends Activity {
    // Declare Variables
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String NAME = "rank";
    static String TYPE = "country";
    static String FLAG = "flag";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from listview_main.xml
        setContentView(R.layout.activity_main);
        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        //@Override
        //protected void onPreExecute() {
          //  super.onPreExecute();
            // Create a progressdialog
            //mProgressDialog = new ProgressDialog(MainActivity.this);

            // Set progressdialog message
            //mProgressDialog.setMessage("Loading...");
            //mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            //mProgressDialog.show();
       // }
        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONParser
                    .getJSONfromURL("http://api.openweathermap.org/data/2.5/weather?id=2172797");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("id");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                   map.put(MainActivity.NAME, jsonobject.getString("main"));
map.put(MainActivity.TYPE, jsonobject.getString("description"));
//map.put(MainActivity.FLAG, jsonobject.getString("restaurantIMAGE"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }
}
ListViewAdapter.java:

public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    //ImageLoader imageLoader;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
      //  imageLoader = new ImageLoader(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView main;
        TextView description;
        //ImageView flag;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        main = (TextView) itemView.findViewById(R.id.main);
        description = (TextView) itemView.findViewById(R.id.description);

        // Locate the ImageView in listview_item.xml
        //flag = (ImageView) itemView.findViewById(R.id.flag);

        // Capture position and set results to the TextViews
        main.setText(resultp.get(MainActivity.NAME));
        description.setText(resultp.get(MainActivity.TYPE));
        // Capture position and set results to the ImageView
        // Passes flag images URL into ImageLoader.class
      //  imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
        // Capture ListView item click
        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                resultp = data.get(position);
                Intent intent = new Intent(context, SingleItemView.class);
                // Pass all data rank
                intent.putExtra("name", resultp.get(MainActivity.NAME));
                // Pass all data country
                intent.putExtra("type", resultp.get(MainActivity.TYPE));
                // Pass all data flag
               // intent.putExtra("flag", resultp.get(MainActivity.FLAG));
                // Start SingleItemView Class
                context.startActivity(intent);

            }
        });
        return itemView;
    }
}
布局中的listview_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/main"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/description"/>
</LinearLayout>


我正在按城市id搜索天气数据。但是得到了这些错误

您遇到未知的异常,这意味着您有连接问题。检查您的internet连接,并检查您是否在应用程序清单中添加了internet权限

在JSONParser类中,您将从getJSONFromURL方法返回一个JSONObject。出于某种原因,很可能是由于连接问题或服务器问题,响应无效。您尝试将该响应读取为JSONObject,但解析失败。因此,您的方法将在AsyncTask中向doInBackground返回null。因此,发生了空指针异常


因此,您应该检查为什么会出现unknownHostException,并修复getJSONFromURL方法中的null问题。此外,还必须正确解析JSON,因为解析过程中也存在Amit Gupta提到的错误。希望能有所帮助。

您遇到未知的异常,这意味着您有连接问题。检查您的internet连接,并检查您是否在应用程序清单中添加了internet权限

在JSONParser类中,您将从getJSONFromURL方法返回一个JSONObject。出于某种原因,很可能是由于连接问题或服务器问题,响应无效。您尝试将该响应读取为JSONObject,但解析失败。因此,您的方法将在AsyncTask中向doInBackground返回null。因此,发生了空指针异常


因此,您应该检查为什么会出现unknownHostException,并修复getJSONFromURL方法中的null问题。此外,还必须正确解析JSON,因为解析过程中也存在Amit Gupta提到的错误。希望能有所帮助。

您遇到未知的异常,这意味着您有连接问题。检查您的internet连接,并检查您是否在应用程序清单中添加了internet权限

在JSONParser类中,您将从getJSONFromURL方法返回一个JSONObject。出于某种原因,很可能是由于连接问题或服务器问题,响应无效。您尝试将该响应读取为JSONObject,但解析失败。因此,您的方法将在AsyncTask中向doInBackground返回null。因此,发生了空指针异常


因此,您应该检查为什么会出现unknownHostException,并修复getJSONFromURL方法中的null问题。此外,还必须正确解析JSON,因为解析过程中也存在Amit Gupta提到的错误。希望能有所帮助。

您遇到未知的异常,这意味着您有连接问题。检查您的internet连接,并检查您是否在应用程序清单中添加了internet权限

在JSONParser类中,您将从getJSONFromURL方法返回一个JSONObject。出于某种原因,很可能是由于连接问题或服务器问题,响应无效。您尝试将该响应读取为JSONObject,但解析失败。因此,您的方法将在AsyncTask中向doInBackground返回null。因此,发生了空指针异常


因此,您应该检查为什么会出现unknownHostException,并修复getJSONFromURL方法中的null问题。此外,还必须正确解析JSON,因为解析过程中也存在Amit Gupta提到的错误。希望有帮助。

使用JSONLint查看您的Json结构

我使用了你的URL“”

并获得如下所示的Json

{
    "coord": {
        "lon": 145.766663,
        "lat": -16.91667
    },
    "sys": {
        "country": "AU",
        "sunrise": 1381952820,
        "sunset": 1381997847
    },
    "weather": [
        {
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }
    ],
    "base": "global stations",
    "main": {
        "temp": 297.15,
        "pressure": 1015,
        "humidity": 88,
        "temp_min": 297.15,
        "temp_max": 297.15
    },
    "wind": {
        "speed": 2.1,
        "deg": 350
    },
    "rain": {
        "3h": 0
    },
    "clouds": {
        "all": 75
    },
    "dt": 1382022000,
    "id": 2172797,
    "name": "Cairns",
    "cod": 200
}
在这一行

jsonarray = jsonobject.getJSONArray("id");//Wrong Key for JsonArray
你犯了错误

应该是

jsonarray = jsonobject.getJSONArray("weather");// weather is key for Array
编辑:

@Override
        protected Void doInBackground(Void... arg0) {
            String url="http://api.openweathermap.org/data/2.5/weather?id=2172797";
            JSONObject json=parser.getJSONFromUrl(url);
            try {
                JSONObject child=json.getJSONObject("coord");
                System.out.println("Child"+child.getString("lon"));
                JSONArray jsonArray=json.getJSONArray("weather");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject wether=jsonArray.getJSONObject(i);
                    System.out.println("Wether"+wether.getString("id")+" "+wether.getString("main"));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
现在,按照同样的方法获取所需的所有值


希望这能对您有所帮助。

使用JSONLint查看您的Json结构

我使用了你的URL“”

并获得如下所示的Json

{
    "coord": {
        "lon": 145.766663,
        "lat": -16.91667
    },
    "sys": {
        "country": "AU",
        "sunrise": 1381952820,
        "sunset": 1381997847
    },
    "weather": [
        {
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }
    ],
    "base": "global stations",
    "main": {
        "temp": 297.15,
        "pressure": 1015,
        "humidity": 88,
        "temp_min": 297.15,
        "temp_max": 297.15
    },
    "wind": {
        "speed": 2.1,
        "deg": 350
    },
    "rain": {
        "3h": 0
    },
    "clouds": {
        "all": 75
    },
    "dt": 1382022000,
    "id": 2172797,
    "name": "Cairns",
    "cod": 200
}
在这一行

jsonarray = jsonobject.getJSONArray("id");//Wrong Key for JsonArray
你犯了错误

应该是

jsonarray = jsonobject.getJSONArray("weather");// weather is key for Array
编辑:

@Override
        protected Void doInBackground(Void... arg0) {
            String url="http://api.openweathermap.org/data/2.5/weather?id=2172797";
            JSONObject json=parser.getJSONFromUrl(url);
            try {
                JSONObject child=json.getJSONObject("coord");
                System.out.println("Child"+child.getString("lon"));
                JSONArray jsonArray=json.getJSONArray("weather");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject wether=jsonArray.getJSONObject(i);
                    System.out.println("Wether"+wether.getString("id")+" "+wether.getString("main"));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
现在,按照同样的方法获取所需的所有值


希望这能对您有所帮助。

使用JSONLint查看您的Json结构

我使用了你的URL“”

并获得如下所示的Json

{
    "coord": {
        "lon": 145.766663,
        "lat": -16.91667
    },
    "sys": {
        "country": "AU",
        "sunrise": 1381952820,
        "sunset": 1381997847
    },
    "weather": [
        {
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }
    ],
    "base": "global stations",
    "main": {
        "temp": 297.15,
        "pressure": 1015,
        "humidity": 88,
        "temp_min": 297.15,
        "temp_max": 297.15
    },
    "wind": {
        "speed": 2.1,
        "deg": 350
    },
    "rain": {
        "3h": 0
    },
    "clouds": {
        "all": 75
    },
    "dt": 1382022000,
    "id": 2172797,
    "name": "Cairns",
    "cod": 200
}
在这一行

jsonarray = jsonobject.getJSONArray("id");//Wrong Key for JsonArray
你犯了错误

应该是

jsonarray = jsonobject.getJSONArray("weather");// weather is key for Array
编辑:

@Override
        protected Void doInBackground(Void... arg0) {
            String url="http://api.openweathermap.org/data/2.5/weather?id=2172797";
            JSONObject json=parser.getJSONFromUrl(url);
            try {
                JSONObject child=json.getJSONObject("coord");
                System.out.println("Child"+child.getString("lon"));
                JSONArray jsonArray=json.getJSONArray("weather");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject wether=jsonArray.getJSONObject(i);
                    System.out.println("Wether"+wether.getString("id")+" "+wether.getString("main"));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
现在,按照同样的方法获取所需的所有值


希望这能对您有所帮助。

使用JSONLint查看您的Json结构

我使用了你的URL“”

并获得如下所示的Json

{
    "coord": {
        "lon": 145.766663,
        "lat": -16.91667
    },
    "sys": {
        "country": "AU",
        "sunrise": 1381952820,
        "sunset": 1381997847
    },
    "weather": [
        {
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }
    ],
    "base": "global stations",
    "main": {
        "temp": 297.15,
        "pressure": 1015,
        "humidity": 88,
        "temp_min": 297.15,
        "temp_max": 297.15
    },
    "wind": {
        "speed": 2.1,
        "deg": 350
    },
    "rain": {
        "3h": 0
    },
    "clouds": {
        "all": 75
    },
    "dt": 1382022000,
    "id": 2172797,
    "name": "Cairns",
    "cod": 200
}
在这一行

jsonarray = jsonobject.getJSONArray("id");//Wrong Key for JsonArray
你犯了错误

应该是

jsonarray = jsonobject.getJSONArray("weather");// weather is key for Array
编辑:

@Override
        protected Void doInBackground(Void... arg0) {
            String url="http://api.openweathermap.org/data/2.5/weather?id=2172797";
            JSONObject json=parser.getJSONFromUrl(url);
            try {
                JSONObject child=json.getJSONObject("coord");
                System.out.println("Child"+child.getString("lon"));
                JSONArray jsonArray=json.getJSONArray("weather");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject wether=jsonArray.getJSONObject(i);
                    System.out.println("Wether"+wether.getString("id")+" "+wether.getString("main"));
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
现在,按照同样的方法获取所需的所有值

希望这对您有所帮助。

您的日志显示:

http连接java.net.UnknownHostException中出错: api.openweathermap.org

您需要在清单xml文件中添加网络权限

它已经在那里了

然后检查您的设备:

  • 您可能处于飞行模式
  • 数据连接已关闭
  • 如果您的手机使用wi-fi,请检查防火墙设置,有什么东西挡住了您

我在模拟器上运行它,而不是在设备上

从SO:

答案非常简单:删除,然后重新创建AVD (虚拟设备/模拟器)在Eclipse中。这对我来说是第一次

你的logcat说:

http连接java.net.UnknownHostException中出错: api.openweathermap.org

您需要在清单xml文件中添加网络权限

它已经在那里了

然后检查您的设备:

  • 您可能处于飞行模式
  • 数据连接已关闭
  • 如果您的手机使用wi-fi,请检查防火墙设置,有什么东西挡住了您

我在模拟器上运行,而不是在设备上运行