我看过这里的其他帖子,但我找不到问题的答案。我正在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)包装initializatin时,代码才会运行所以我想没有检索到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)
答案 0 :(得分:2)
这是因为,正如异常所述,您正试图在主线程上访问互联网。 Android不允许这样做,因为它可能会滞后于UI。因此,您需要在不是UI线程的单独线程上连接到Internet。请参阅http://developer.android.com/reference/android/os/AsyncTask.html(您只需要doInBackground进行互联网访问)。
答案 1 :(得分:2)
当您将代码移动到(例如)AsyncTask中时,请从您的网址中删除“s”。它应该是http而不是https,否则你将获得404而没有数据。