我接收服务器响应为JSON格式,并且只需要获取字段" text"并将其存储在arraylist中。我试过,但我只得到第一个文本字段显示在输出下面,但我需要证据列表json对象中的所有文本字段。任何人都可以帮助我获取所有文本字段吗?
Android代码:
HttpEntity entity = response.getEntity(); // wait for response
// convert response to a string and log it
if (entity != null) {
//Toast.makeText(RegistartionPage.this,
// "Inside If", Toast.LENGTH_LONG)
//.show();
InputStream inputstream = entity.getContent();
BufferedReader bufferedreader =
new BufferedReader(new InputStreamReader(inputstream));
StringBuilder stringbuilder = new StringBuilder();
String currentline = null;
while ((currentline = bufferedreader.readLine()) != null) {
//Toast.makeText(RegistartionPage.this,
// "Inside while", Toast.LENGTH_LONG)
// .show();
stringbuilder.append(currentline + "\n");
}
String result = stringbuilder.toString();
Log.v("HTTP REQUEST",result);
inputstream.close();
ids=result;
}
JSONArray jsonArray = new JSONArray(ids);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String question1 = c.getString("question");
JSONObject object2 = new JSONObject(question1);
JSONArray jArray1 = object2.getJSONArray("evidencelist");
//String comp_id = jArray1.optString();
for(int j = 0; j < jArray1 .length(); j++)
{
JSONObject object3 = jArray1.getJSONObject(j);
text = object3.getString("text");
/*num = object3.getInt("value");*/
}
}
服务器的JSON输出:
09-19 17:00:41.182: V/HTTP REQUEST(1539): [
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "question": {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "qclasslist": [
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value": "FACTOID"
09-19 17:00:41.182: V/HTTP REQUEST(1539): },
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value": "DESCRIPTIVE"
09-19 17:00:41.182: V/HTTP REQUEST(1539): }
09-19 17:00:41.182: V/HTTP REQUEST(1539): ],
09-19 17:00:41.182: V/HTTP REQUEST(1539): "focuslist": [
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value": "What"
09-19 17:00:41.182: V/HTTP REQUEST(1539): }
09-19 17:00:41.182: V/HTTP REQUEST(1539): ],
09-19 17:00:41.182: V/HTTP REQUEST(1539): "latlist": [
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value": "HIV"
09-19 17:00:41.182: V/HTTP REQUEST(1539): }
09-19 17:00:41.182: V/HTTP REQUEST(1539): ],
09-19 17:00:41.182: V/HTTP REQUEST(1539): "evidencelist": [
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value""0.90134596824646",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "text": "HIV stands for human immunodeficiency virus. This is the virus that causes AIDS. There is no cure yet for HIV/AIDS, but there are treatments that can help people live longer, healthier lives.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "id": "PB_472D471A96B201B91949C3B1C28D468B",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "title": "Get Tested for HIV : Get Tested for HIV : The Basics : What is HIV?",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "document":"http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B91949C3B1C28D468B/175/3441" (http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B91949C3B1C28D468B/175/3441%27) ,
09-19 17:00:41.182: V/HTTP REQUEST(1539): "copyright": "No copyright specified for this supporting passage or document.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "termsOfUse": "No license specified for this supporting passage or document.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "metadataMap": {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "abstract": "Protect yourself and others from HIV and other STDs.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "originalfile": "Get Tested for HIV.html",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "title": "Get Tested for HIV : Get Tested for HIV : The Basics : What is HIV?",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "corpusName": "PB",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "description": "Protect yourself and others from HIV and other STDs.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "deepqaid": "33646738",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "fileName": "/watsondata/ingestion/14d9d1d3b23/working/201505290044/eng/di/output/trec/intermediate/xml-splitTrecTrim/33646738.xml",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "DOCNO": "472D471A96B201B91949C3B1C28D468B"
09-19 17:00:41.182: V/HTTP REQUEST(1539): }
09-19 17:00:41.182: V/HTTP REQUEST(1539): },
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value":"0.06850375980138779",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "text": "Breastfeeding, pregnancy, or childbirth if the mother has HIV. Getting a transfusion of blood that's infected with HIV (very rare in the United States). Learn more about HIV/AIDS: What is HIV/AIDS? Women and HIV/AIDS. Take Action! Pages: 1 2 3 4 5 6 >.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "id":"PB_472D471A96B201B944EEF8045EC5E0AF",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "title": "Get Tested for HIV : Get Tested for HIV : The Basics : How do people get HIV?",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "document":"http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B944EEF8045EC5E0AF/473/725" (http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_472D471A96B201B944EEF8045EC5E0AF/473/725%27) ,
09-19 17:00:41.182: V/HTTP REQUEST(1539): "copyright": "No copyright specified for this supporting passage or document.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "termsOfUse": "No license specified for this supporting passage or document.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "metadataMap": {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "abstract": "Protect yourself and others from HIV and other STDs.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "originalfile": "Get Tested for HIV.html",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "title": "Get Tested for HIV : Get Tested for HIV : The Basics : How do people get HIV?",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "corpusName": "PB",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "description": "Protect yourself and others from HIV and other STDs.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "deepqaid": "33646739",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "fileName": "/watsondata/ingestion/14d9d1d3b23/working/201505290044/eng/di/output/trec/intermediate/xml-splitTrecTrim/33646739.xml",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "DOCNO": "472D471A96B201B944EEF8045EC5E0AF"
09-19 17:00:41.182: V/HTTP REQUEST(1539): }
09-19 17:00:41.182: V/HTTP REQUEST(1539): },
09-19 17:00:41.182: V/HTTP REQUEST(1539): {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "value":"0.03481652960181236",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "text": "See a doctor right away if you have unprotected anal or vaginal sex with someone who might be HIV-positive. Taking medication within 3 days (known as post-exposure prophylaxis or PEP) can dramatically reduce the chance of getting HIV. Talk to your doctor about HIV medication to prevent HIV infection (known as pre-exposure prophylaxis or PrEP) if you often have unprotected anal or vaginal sex. If you are HIV-positive, tell your sex partners you have HIV before you have sex with them and take steps to prevent transmitting HIV to others.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "id": "PB_8C42FE3B49104EC939E170D23ADBCDD7",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "title": "National Latino AIDS Awareness Day : National Latino AIDS Awareness Day : National Latino AIDS Awareness Day :What Can You Do?",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "document":"http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_8C42FE3B49104EC939E170D23ADBCDD7/1379/1919" (http://10.110.76.161:8080/instance/67/deepqa/v1/question/document/PB_8C42FE3B49104EC939E170D23ADBCDD7/1379/1919%27) ,
09-19 17:00:41.182: V/HTTP REQUEST(1539): "copyright": "No copyright specified for this supporting passage or document.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "termsOfUse": "No license specified for this supporting passage or document.",
09-19 17:00:41.182: V/HTTP REQUEST(1539): "metadataMap": {
09-19 17:00:41.182: V/HTTP REQUEST(1539): "abstract": "HIV continues to threaten the health of Hispanic and Latinos in the United States. CDC
09-19 17:00:41.249: I/Choreographer(1539): Skipped 12476 frames! The application may be doing too much work on its main thread.
答案 0 :(得分:1)
试试这个..
obj.getAllUser()