【发布时间】:2016-07-05 11:22:15
【问题描述】:
以下是我的 JSON 响应
[
{
"msg":"4,MADHUVAN,5,AMBAMATA,6,PH-1,7,PH-2",
"Value1":null,
"Value2":null,
"Value3":null,
"Value4":null,
"Value5":null,
"Value6":null,
"Value7":null,
"Value8":null,
"Value9":null,
"Value10":null
}
]
下面是我的 Async_Task -
class async_Division extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
SoapObject request = new SoapObject(namespacedivision, method_name__filldivision);
request.addProperty(parameter_filldivision, circle);//add the parameters
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(url_division);
androidHttpTransport.call(soap_action_filldivision, envelope); // this is the actual part that will call the webservice
//SoapPrimitive prim = (SoapPrimitive) envelope.getResponse(); // Get the SoapResult from the envelope body.
SoapObject response = (SoapObject) envelope.bodyIn;
//resultstate = prim.toString();
resultDivision = response.getPropertyAsString(0).toString();
if (resultDivision != null) {
try {
JSONArray resultarray = new JSONArray(resultDivision);
for (int i = 0; i < resultarray.length(); i++) {
JSONObject c = resultarray.getJSONObject(i);
String idLocality = c.getString("msg");
String nameLocality = (c.getString("msg"));
// Split data by comma
String[] namesList = idLocality.split(",");
String idDivision = namesList[0];
String nameDivision = namesList[1];
//cityname.add(cityName);
HashMap<String, String> hashlocality = new HashMap<String, String>();
hashlocality.put(TAG_ID_fillcombodivisionid, idDivision);
hashlocality.put(TAG_Name_fillcombodivisionname, nameDivision);
resultListDivision.add(hashlocality);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} catch (Exception e) {
e.printStackTrace();
}
return resultDivision;
}
@Override
protected void onPostExecute(String result1) {
// TODO Auto-generated method stub
SpinnerAdapter adapter = new SimpleAdapter(getActivity(), resultListDivision, R.layout.activity_showspinner_division, new String[]{TAG_ID_fillcombodivisionid, TAG_Name_fillcombodivisionname}, new int[]{R.id.textidlocality, R.id.textnamelocality});
spnDivision.setAdapter(adapter);
spnDivision.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
//save.setClickable(true);
TextView txtid = (TextView) arg0.findViewById(R.id.textidlocality);
division = txtid.getText().toString();
// Call web service for district
//callvillage();
new async_Sub_Division().execute();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
我在我的Spinner 中填充上面的 JSON 响应。一切正常,但我的Spinner 只显示一个值“MADHUVAN”。我想在我的Spinner 中显示所有JSONArray,我还想在零位置显示默认的硬编码文本,如“选择”。谁能解决这个问题?
【问题讨论】:
-
这是因为您的 json 中只有一个值。
-
感谢 Shane 的回复,但上面我提到了我的 Json 回复和更多价值,例如“MADHUVAN,AMBAMATA”,但它只显示“MADHUVAN”。
-
那么您的服务器响应中存在一些问题。不在这里
-
这是因为你用逗号分割字符串,所以只有第一个值来找你。
标签: android json web-services spinner