【问题标题】:Gson parsing during asyntask [closed]异步任务期间的 Gson 解析 [关闭]
【发布时间】:2013-06-27 15:47:10
【问题描述】:

我正在做一个请求,它给了我以下答案:

{
  "SuL":[
    {"IdS":"63","Nam":"Quiz1","Cat":"4097"},
    {"IdS":"64","Nam":"6e","Cat":"4099"},
    {"IdS":"65","Nam":"CP","Cat":"4100"},
    {"IdS":"66","Nam":"CE1","Cat":"4098"}
  ],    
  "CaL":[
    {"Cod":"4097","Lab":"Categorie1"},
    {"Cod":"4098","Lab":"Math"},
    {"Cod":"4099","Lab":"Anglais"},
    {"Cod":"4100","Lab":"Géographie"}
  ]
}

我想要做的是将所有“实验室”值放在一个字符串列表中。 SuL 表示服务器上可用对象的列表(我现在对此不感兴趣),CaL 表示类别。

这是我的类别课程:

public class Categorie {

  public String Cod;
  public String Lab;

  public String getCode() {
    return Cod;
  }
  public void setCode(String code) {
    this.Cod = code;
  }
  public String getName() {
    return Lab;
  }
  public void setName(String name) {
    this.Lab = name;
  }
}

这是我的 AsyncTask 执行请求和 Gson 解析:

class DownloadQuizCategory extends AsyncTask<String, String, String> {

  protected String doInBackground(String...uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    ByteArrayOutputStream out;
    try {
      response = httpclient.execute(new HttpGet(uri[0]));
      StatusLine statusLine = response.getStatusLine();
      if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        out.close();
        responseString = out.toString();
      } else {
        // Closes the connection.
        response.getEntity().getContent().close();
        throw new IOException(statusLine.getReasonPhrase());
      }
    } catch (ClientProtocolException e) {
      // TODO Handle problems..
    } catch (IOException e) {
      // TODO Handle problems..
    }
    Log.i("AsyncTask", "responseString : " + responseString);
    return responseString;
  }

  @Override
  protected void onPostExecute(String resultat) {
    super.onPostExecute(resultat);
    //PARSING HERE :(
  }
}

这是由 :

调用的
new DownloadQuizCategory().execute(URL_category); //where my results are

【问题讨论】:

  • 您能否重新表述您的问题,告诉我们您使用此代码获得的输出是什么以及您期望获得什么?
  • 我在这里回答另一个关于解析的问题,看看吧。 stackoverflow.com/questions/17294844/…

标签: java android parsing http gson


【解决方案1】:

您只需要在 Categorie 类之外创建另一个类。像这样的:

public class Response {
    private List<Categorie> CaL;
    //getter & setter
}

然后解析您的响应:

Gson gson = new Gson();
Response response = gson.fromJson(resultat, Response.class);

Gson 将自动且静默地将所有值跳过到 "SuL" 中,因为在您的 Response 类中没有名为 SuL 的属性...


编辑:然后,如果您希望将所有 "Lab" vlaues 转换为 List&lt;String&gt;,您只需要这样做(这是 Java 基础...):

List<String> labs = new ArrayList<String>();
for (Categorie c : response.getCaL()) {
   labs.add(c.getName());
}

【讨论】:

  • 非常感谢,效果很好
【解决方案2】:

你必须反序列化你的 Json,试试上面的这几行。您的 JSONArray Lab 应该包含您的“实验室”列表。

@Override
protected void onPostExecute(String resultat)
{
    super.onPostExecute(resultat);
    JSONObject Response = new JSONObject(resultat);
    JSONArray Lab= (JSONArray) Response.get("Lab");
    System.out.print(Lab);
}

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    相关资源
    最近更新 更多