Json URL に接続し、GSON ライブラリを使用してコンテンツを解析する方法に関するチュートリアルを完了しました 。コードはチュートリアルで指定された url で正常に動作しますが、この Urlで動作するように変更しようとしました。2 番目の ULR から 1 つのフィールド、つまりベース フィールドのみを表示しようとしましたが、うまくいきません。これが私のコードです、助けてください:
public class JsonParsingActivity extends Activity {
String url = "http://api.openweathermap.org/data/2.5/weather?q=London,uk";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputStream source = retrieveStream(url);
Gson gson = new Gson();// main Gson class
Reader reader = new InputStreamReader(source);
SearchResponse response = gson.fromJson(reader, SearchResponse.class); //gson.fromJson(String, Class)
Toast.makeText(this, response.base, Toast.LENGTH_SHORT).show();
}
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
}
そして SearchResponse クラス:
public class SearchResponse {
@SerializedName("base")
public String base;
}
ログファイル:
05-21 14:11:35.559: E/AndroidRuntime(15079): FATAL EXCEPTION: main
05-21 14:11:35.559: E/AndroidRuntime(15079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myproject/com.example.myproject.JsonParsingActivity}: java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <com.example.myproject.JsonParsingActivity$1@405494a8> is of type com.example.myproject.JsonParsingActivity$1
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.os.Looper.loop(Looper.java:123)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-21 14:11:35.559: E/AndroidRuntime(15079): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 14:11:35.559: E/AndroidRuntime(15079): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-21 14:11:35.559: E/AndroidRuntime(15079): at dalvik.system.NativeStart.main(Native Method)
05-21 14:11:35.559: E/AndroidRuntime(15079): Caused by: java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <com.example.myproject.JsonParsingActivity$1@405494a8> is of type com.example.myproject.JsonParsingActivity$1
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.google.gson.internal.$Gson$Types.getRawType($Gson$Types.java:145)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:73)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.google.gson.reflect.TypeToken.get(TypeToken.java:296)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.google.gson.Gson.fromJson(Gson.java:801)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.google.gson.Gson.fromJson(Gson.java:768)
05-21 14:11:35.559: E/AndroidRuntime(15079): at com.example.myproject.JsonParsingActivity.onCreate(JsonParsingActivity.java:42)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 14:11:35.559: E/AndroidRuntime(15079): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-21 14:11:35.559: E/AndroidRuntime(15079): ... 11 more
05-21 14:11:44.729: E/AndroidRuntime(15160): FATAL EXCEPTION: main
05-21 14:11:44.729: E/AndroidRuntime(15160): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myproject/com.example.myproject.JsonParsingActivity}: java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <com.example.myproject.JsonParsingActivity$1@40547490> is of type com.example.myproject.JsonParsingActivity$1
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.os.Looper.loop(Looper.java:123)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-21 14:11:44.729: E/AndroidRuntime(15160): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 14:11:44.729: E/AndroidRuntime(15160): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-21 14:11:44.729: E/AndroidRuntime(15160): at dalvik.system.NativeStart.main(Native Method)
05-21 14:11:44.729: E/AndroidRuntime(15160): Caused by: java.lang.IllegalArgumentException: Expected a Class, ParameterizedType, or GenericArrayType, but <com.example.myproject.JsonParsingActivity$1@40547490> is of type com.example.myproject.JsonParsingActivity$1
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.google.gson.internal.$Gson$Types.getRawType($Gson$Types.java:145)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:73)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.google.gson.reflect.TypeToken.get(TypeToken.java:296)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.google.gson.Gson.fromJson(Gson.java:801)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.google.gson.Gson.fromJson(Gson.java:768)
05-21 14:11:44.729: E/AndroidRuntime(15160): at com.example.myproject.JsonParsingActivity.onCreate(JsonParsingActivity.java:42)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-21 14:11:44.729: E/AndroidRuntime(15160): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-21 14:11:44.729: E/AndroidRuntime(15160): ... 11 more