0

次のように HttpResponse を介してキャプチャする型指定された配列リストを送信する Web サービスがあります。

// create GET request
HttpGet httpGet = new HttpGet("http://localhost:8084/MinecraftRestServer/webresources/Items");
// execute GET request
HttpResponse response = client.execute(httpGet);
// check response
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // response OK
    // retreive response
    List<Recipe> recipesList = new ArrayList<Recipe>();
    HttpEntity jsonObj = response.getEntity();
            //What's next?

Web サービスから送信される配列は次のようになります。

recipesList.add(new Item(1, 11, "diamond_ingot", "Diamond ingot",
                "0,0,0,0,0,0,0,0,1", "air,diamond_ore"));
recipesList.add(new Item(2, 11, "iron_ingot", "Iron ingot",
                "0,0,0,0,0,0,0,0,1", "air,iron_ore"));

そして、この形式で出てきます:

[{"recipeCategory":11,"recipeImageID":"diamond_ingot","recipeDescription":"Diamond ingot","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,diamond_ore","recipeID":1},{"recipeCategory":11,"recipeImageID":"iron_ingot","recipeDescription":"Iron ingot","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,iron_ore","recipeID":2},{"recipeCategory":11,"recipeImageID":"gold_ingot","recipeDescription":"Gold ingot","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,gold_ore","recipeID":3},{"recipeCategory":11,"recipeImageID":"diamond_ore","recipeDescription":"Diamond ore","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,wooden_pickaxe","recipeID":4},{"recipeCategory":11,"recipeImageID":"iron_ore","recipeDescription":"Iron ore","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,wooden_pickaxe","recipeID":5},{"recipeCategory":11,"recipeImageID":"gold_ore","recipeDescription":"Gold ore","recipeLocations":"0,0,0,0,0,0,0,0,1","usedImages":"air,wooden_pickaxe","recipeID":6},{"recipeCategory":2,"recipeImageID":"diamond_boots","recipeDescription":"Boots (Diamond)","recipeLocations":"0,0,0,1,0,1,1,0,1","usedImages":"air,diamond_ingot","recipeID":7},{"recipeCategory":2,"recipeImageID":"gold_boots","recipeDescription":"Boots (Gold)","recipeLocations":"0,0,0,1,0,1,1,0,1","usedImages":"air,gold_ingot","recipeID":8},{"recipeCategory":2,"recipeImageID":"iron_boots","recipeDescription":"Boots (Iron)","recipeLocations":"0,0,0,1,0,1,1,0,1","usedImages":"air,iron_ingot","recipeID":9},{"recipeCategory":2,"recipeImageID":"diamond_leggings","recipeDescription":"Leggings (Diamond)","recipeLocations":"1,1,1,1,0,1,1,0,1","usedImages":"air,diamond_ingot","recipeID":10},{"recipeCategory":2,"recipeImageID":"gold_leggings","recipeDescription":"Leggings (Gold)","recipeLocations":"1,1,1,1,0,1,1,0,1","usedImages":"air,gold_ingot","recipeID":11},{"recipeCategory":2,"recipeImageID":"iron_leggings","recipeDescription":"Leggings (Iron)","recipeLocations":"1,1,1,1,0,1,1,0,1","usedImages":"air,iron_ingot","recipeID":12},{"recipeCategory":2,"recipeImageID":"diamond_chestplate","recipeDescription":"Chestplate (Diamond)","recipeLocations":"1,0,1,1,1,1,1,1,1","usedImages":"air,diamond_ingot","recipeID":13},{"recipeCategory":2,"recipeImageID":"gold_chestplate","recipeDescription":"Chestplate (Gold)","recipeLocations":"1,0,1,1,1,1,1,1,1","usedImages":"air,gold_ingot","recipeID":14},{"recipeCategory":2,"recipeImageID":"iron_chestplate","recipeDescription":"Chestplate (Iron)","recipeLocations":"1,0,1,1,1,1,1,1,1","usedImages":"air,iron_ingot","recipeID":15},{"recipeCategory":2,"recipeImageID":"diamond_helmet","recipeDescription":"Helmet (Diamond)","recipeLocations":"1,1,1,1,0,1,0,0,0","usedImages":"air,diamond_ingot","recipeID":16},{"recipeCategory":2,"recipeImageID":"gold_helmet","recipeDescription":"Helmet (Gold)","recipeLocations":"1,1,1,1,0,1,0,0,0","usedImages":"air,gold_ingot","recipeID":17},{"recipeCategory":2,"recipeImageID":"iron_helmet","recipeDescription":"Helmet 

私の質問は、これを arraylist ( ArrayList<Item>) に戻すにはどうすればよいかということです。クライアント アプリケーションには既に Item クラスが存在します。

Gson ライブラリに関する例を読んだことがありますが、API 17 でコンパイルすると含まれなくなったようです。

最も簡単なアプローチは何ですか?

4

6 に答える 6

1

Eclipse を使用している場合は、ここGSONから jar をダウンロードしてプロジェクトに含めます。

Android Studio を使用している場合は、を開き、以下をブロックbuild.gradleに追加します。dependenciesまたは、maven を使用しないことを選択して、単純に jar を lib フォルダーにドロップすることもできます。

compile 'com.google.code.gson:gson:2.2.4'

次に、 を使用GSONしてアイテムのリストを作成します。応答Item.javaと同じメンバー名を持つクラスがあることを確認してくださいJSON

 List<Recipe> recipesList = new ArrayList<Recipe>();
 HttpEntity jsonObj = response.getEntity();
 String data = EntityUtils.toString(entity);
 Log.d("TAG", data);
 Gson gson = new GsonBuilder().create();
 recipesList = gson.fromJson(data, new TypeToken<List<Item>>() {}.getType());

例外を適切に処理するようにしてください。

于 2013-10-28T18:53:41.543 に答える
0

多くのユーザーが言ったように使用できますGson。これは、を使用した RESTfull クライアントの例ですGson

public class RestRequest {
    Gson gson = new Gson();

    public <T> T post(String url, Class<T> clazz,
        List<NameValuePair> parameters) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {
        // Add your data
        httppost.setEntity(new UrlEncodedFormEntity(parameters));
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        StringBuilder json = inputStreamToString(response.getEntity()
                .getContent());
        T gsonObject = gson.fromJson(json.toString(), clazz);
        return gsonObject;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
    }

    // Fast Implementation
    private StringBuilder inputStreamToString(InputStream is)
        throws IOException {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    // Read response until the end
    while ((line = rd.readLine()) != null) {
        total.append(line);
    }

    // Return full string
    return total;
    }

}

使用法は次のようになります。 new RestRequest("myserver.com/rest/somewebservice", SomeClass.class, Arrays.asList(new BasicValuePair("postParameter", "someParameterValue")));

あなたの場合はどこSomeClass.classになりRecipe[].classますか。サーバー側のエラーを適切に処理するには、この質問も確認してください。

于 2013-10-29T04:07:17.500 に答える
0

Jacksonを使用して、着信 JSON を解析できます。(簡単な紹介

適切なプロパティを持つクラスが既にある場合は、次のように簡単にできます。

public class Items {
    private List<Item> items;
    // getter+setter
}

ObjectMapper mapper = new ObjectMapper();
Items = mapper.readValue(src, Items.class);

詳細については、これを参照してください。

于 2013-10-28T18:41:01.247 に答える
-1

男、グーグルはあなたの友達です!「android json」または「android json parse」をすばやく検索すると、このような素晴らしいチュートリアルがいくつか見つかります

于 2013-10-28T18:40:20.507 に答える