gpt4 book ai didi

java - Android JSON 对象返回 null

转载 作者:行者123 更新时间:2023-12-01 18:19:06 24 4
gpt4 key购买 nike

我查看了这里的其他帖子,但没有找到我的问题的答案。我正在 Android 中练习 JSON,为此我使用了 Forecast.io API。 JSON 如下所示:https://api.forecast.io/forecast/7d5566f0ebf0fe263426f12b52d5c51c/37.8267,-122.423 .

我得到了代码来帮助我从这里的另一篇文章开始:

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double LONGITUDE = location.getLongitude();
double LATITUDE = location.getLatitude();

DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("https://api.forecast.io/forecast/7d5566f0ebf0fe263426f12b52d5c51c/" + LATITUDE + "," + LONGITUDE);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();


} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}




try {
JSONObject j = new JSONObject(result);
String timezone = j.getString("timezone");
text.setText(timezone + "");

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

这一行给了我一个错误:

JSONObject j = new JSONObject(result);

它给出了空指针异常。该代码仅在我用 if(result!=null) 包装初始化时运行,所以我猜没有检索到 JSON。我可能做错了什么?

编辑:这是堆栈跟踪

 01-21 15:28:42.857: W/System.err(31328):android.os.NetworkOnMainThreadException
01-21 15:28:42.857: W/System.err(31328):at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
01-21 15:28:42.857: W/System.err(31328):at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-21 15:28:42.857: W/System.err(31328):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-21 15:28:42.857: W/System.err(31328):at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-21 15:28:42.857: W/System.err(31328):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-21 15:28:42.857: W/System.err(31328):at com.example.weatherapp.MainActivity.onCreate(MainActivity.java:48)
01-21 15:28:42.857: W/System.err(31328):at android.app.Activity.performCreate(Activity.java:5231)

最佳答案

这是因为,正如异常所述,您正在尝试在主线程上访问互联网。 Android 不允许这样做,因为它可能会滞后于 UI。因此,您需要在 UI 线程之外的单独线程上连接到 Internet。引用http://developer.android.com/reference/android/os/AsyncTask.html (您实际上只需要 doInBackground 来访问互联网)。

关于java - Android JSON 对象返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28076964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com