进入Android并尝试按照以下教程学习使用OpenWeatherMap api http://code.tutsplus.com/tutorials/create-a-weather-app-on-android--cms-21587
的天气应用程序我目前遇到以下课程的问题;调用getJSON()方法时返回的JSON对象始终为null,导致不显示天气。网址有效,我不确定我做错了什么;我想我可能在connection.addRequestProperty行中遇到某种问题。我不确定此JSON解析实际上是否需要API密钥,因为您可以在浏览器中获得结果,例如。 http://api.openweathermap.org/data/2.5/weather?q=London&units=metric
如果有人对我出错的地方有任何了解,我们将非常感激!
编辑:如果有人需要看到包的其余部分,那么它目前与发布的链接中包含的代码完全相同。
编辑2:添加了在dieter_h的建议更改后创建的logcat,感谢man。
08-20 23:20:55.219 12169-12169/? I/art﹕ Late-enabling -Xcheck:jni
08-20 23:20:55.587 12169-12191/simpleweather.ockmore.will.simpleweather D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-20 23:20:55.598 12169-12169/simpleweather.ockmore.will.simpleweather D/Atlas﹕ Validating map...
08-20 23:20:55.659 12169-12188/simpleweather.ockmore.will.simpleweather I/myActivity﹕ data{"cod":"404","message":"Error: Not found city"}
08-20 23:20:55.671 12169-12191/simpleweather.ockmore.will.simpleweather I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020_msm8960_LA.AF.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 03/30/15 Mon
Local Branch: mybranch8688311
Remote Branch: quic/LA.AF.1.1_rb1.16
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020 + 9b2699f + 2215637 + 60aa592 + f2362e6 + 5c64f59 + 82411a1 + 1f36e07 + NOTHING
08-20 23:20:55.673 12169-12191/simpleweather.ockmore.will.simpleweather I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-20 23:20:55.707 12169-12191/simpleweather.ockmore.will.simpleweather D/OpenGLRenderer﹕ Enabling debug mode 0
08-20 23:20:55.809 12169-12169/simpleweather.ockmore.will.simpleweather E/SimpleWeather﹕ One or more fields not found in JSON data
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
public class RemoteFetch {
public static JSONObject getJSON(Context context, String city){
try {
URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=" +city+ "&units=metric");
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();
connection.addRequestProperty("x-api-key",
context.getString(R.string.open_weather_maps_app_id));
BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuilder json = new StringBuilder(1024);
String tmp=" ";
while ((tmp=reader.readLine())!=null)
json.append(tmp).append("\n");
reader.close();
JSONObject data = new JSONObject(json.toString());
//This value will be 404 if the request was not
//successful
if (data.getInt("cod")!=200){
return null;
}
return data;
}catch(Exception e) {
return null;
}
}
}
编辑3:下面添加了片段和活动类
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;
import org.json.JSONObject;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
public class WeatherFragment extends Fragment{
Typeface weatherFont;
TextView cityField;
TextView updatedField;
TextView detailsField;
TextView currentTemperatureField;
TextView weatherIcon;
Handler handler;
public WeatherFragment(){
handler = new Handler();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_weather, container, false);
cityField = (TextView)rootView.findViewById(R.id.city_field);
updatedField = (TextView)rootView.findViewById(R.id.updated_field);
detailsField = (TextView)rootView.findViewById(R.id.details_field);
currentTemperatureField = (TextView)rootView.findViewById(R.id.current_temperature_field);
weatherIcon = (TextView)rootView.findViewById(R.id.weather_icon);
weatherIcon.setTypeface(weatherFont);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/weather.ttf");
updateWeatherData(new CityPreference(getActivity()).getCity());
}
private void updateWeatherData(final String city){
new Thread(){
public void run(){
final JSONObject json = RemoteFetch.getJSON(getActivity(), city);
if (json == null){
handler.post(new Runnable(){
public void run(){
Toast.makeText(getActivity(),
getActivity().getString(R.string.place_not_found),
Toast.LENGTH_LONG).show();
}
});
} else {
handler.post(new Runnable(){
@Override
public void run() {
renderWeather(json);
}
});
}
}
}.start();
}
private void renderWeather(JSONObject json){
try {
cityField.setText(json.getString("name").toUpperCase(Locale.UK) +
"," +
json.getJSONObject("sys").getString("country"));
JSONObject details = json.getJSONArray("weather").getJSONObject(0);
JSONObject main = json.getJSONObject("main");
detailsField.setText(
details.getString("description").toUpperCase(Locale.UK) +
"\n" + "Humidity: " + main.getString("humidity") + "%" +
"\n" + "Pressure: " + main.getString("pressure") + "hPa");
currentTemperatureField.setText(
String.format("%.2f", main.getDouble("temp")) + " ℃");
DateFormat df = DateFormat.getDateInstance();
String updatedOn = df.format(new Date(json.getLong("dt")*1000));
updatedField.setText("Last update: " + updatedOn);
setWeatherIcon(details.getInt("id"),
json.getJSONObject("sys").getLong("sunrise") * 1000,
json.getJSONObject("sys").getLong("sunset") * 1000);
}catch (Exception e) {
Log.e("SimpleWeather", "One or more fields not found in JSON data");
}
}
private void setWeatherIcon(int actualId, long sunrise, long sunset){
int id = actualId / 100;
String icon = "";
if(actualId == 800){
long currentTime = new Date().getTime();
if (currentTime>=sunrise && currentTime<sunset) {
icon = getActivity().getString(R.string.weather_sunny);
} else {
icon = getActivity().getString(R.string.weather_clear_night);
}
} else {
switch (id) {
case 2 : icon = getActivity().getString(R.string.weather_thunder);
break;
case 3 : icon = getActivity().getString(R.string.weather_drizzle);
break;
case 7 : icon = getActivity().getString(R.string.weather_foggy);
break;
case 8 : icon = getActivity().getString(R.string.weather_cloudy);
break;
case 6 : icon = getActivity().getString(R.string.weather_snowy);
break;
case 5 : icon = getActivity().getString(R.string.weather_rainy);
break;
}
weatherIcon.setText(icon);
}
}
public void changeCity(String city){
updateWeatherData(city);
}
格式化有点奇怪,抱歉。
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.widget.EditText;
public class WeatherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
if(savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WeatherFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_weather, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.change_city) {
showInputDialog();
}
return false;
}
private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change city");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
changeCity(input.getText().toString());
}
});
builder.show();
}
public void changeCity(String city){
WeatherFragment wf = (WeatherFragment)getSupportFragmentManager()
.findFragmentById(R.id.container);
wf.changeCity(city);
new CityPreference(this).setCity(city);
}
}
答案 0 :(得分:0)
试试这个,在while循环后添加。
finally
{
try {
if (reader != null)
reader.close();
}// end try
catch (IOException ex)
{
}// end catch
}//end finally.
答案 1 :(得分:0)
请使用addRequestProperty
至setRequestProperty
:
connection.setRequestProperty("x-api-key",
context.getString(R.string.open_weather_maps_app_id));
而不是:
connection.addRequestProperty("x-api-key",
context.getString(R.string.open_weather_maps_app_id));
答案 2 :(得分:0)
更改代码并发布您的logcat:
JSONObject data = new JSONObject(json.toString());
Log.i("MyActivity", "data + " data);
[...]
return data;
}catch(Exception e) {
Log.e("MyActivity", "Exception", e.fillInStackTrace());
return null;
}