Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java asyntask期间的Gson解析_Java_Android_Parsing_Http_Gson - Fatal编程技术网

Java asyntask期间的Gson解析

Java asyntask期间的Gson解析,java,android,parsing,http,gson,Java,Android,Parsing,Http,Gson,我正在做一个请求,该请求给出了以下答案: { "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":"Ca

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

{
  "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"}
  ]
}
我试图做的是将所有“Lab”值放在一个字符串列表中。 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 :(
  }
}

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


您只需在
分类
类之外创建另一个类。大概是这样的:

public class Response {
    private List<Categorie> CaL;
    //getter & setter
}
Gson将自动且无提示地将所有值跳过到
“SuL”
,因为在
响应中没有名为
SuL
的属性


编辑:然后,如果您想将所有的
“Lab”
vlaues放入
列表中,只需执行以下操作(这是Java基础知识…):

List labs=new ArrayList();
for(类别c:response.getCaL()){
添加(c.getName());
}

您能否重新表述您的问题,告诉我们您使用此代码获得了什么输出以及您希望获得什么?我在这里回答另一个有关解析的问题,请查看。非常感谢,效果很好
@Override
protected void onPostExecute(String resultat)
{
    super.onPostExecute(resultat);
    JSONObject Response = new JSONObject(resultat);
    JSONArray Lab= (JSONArray) Response.get("Lab");
    System.out.print(Lab);
}
public class Response {
    private List<Categorie> CaL;
    //getter & setter
}
Gson gson = new Gson();
Response response = gson.fromJson(resultat, Response.class);
List<String> labs = new ArrayList<String>();
for (Categorie c : response.getCaL()) {
   labs.add(c.getName());
}