我连接到Web服务并下拉JSON对象。
我的班级
public class UserLogin implements Serializable
{
public int objMobileCapabilityId;
public int objMobileCapabilityValue;
public String objMobileCapabilityDesc;
public int objMobileDisplayOrder;
public UserLogin(){}
public UserLogin(int objmobilecapabilityid, int objmobilecapabilityvalue, String objmobilecapabilitydesc, int objmobiledisplayorder) {
objMobileCapabilityId = objmobilecapabilityid;
objMobileCapabilityValue = objmobilecapabilityvalue;
objMobileCapabilityDesc = objmobilecapabilitydesc;
objMobileDisplayOrder = objmobiledisplayorder;
}
}
然后,我使用TAGS
获取该数据并创建UserLogin arraylistArrayList<UserLogin> listofCapabilities = new ArrayList<UserLogin>();
try {
// Invole web service
androidHttpTransport.call(NAMESPACE+METHOD_NAME, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String responseJSON=response.toString();
JSONArray jarray =new JSONArray(responseJSON);
ACount = (jarray.length() -1);
for (int i = 0; i <= ACount; i++) {
Description = jarray.getJSONObject(i).getString(TAG_Description);
if(Description.equals("Not a Valid Mobile User")){
userOK = false;
}else {
userOK = true;
objuserlogin.setProperty(2, Description);
listofCapabilities.add(i, objuserlogin);
objuserlogin = new UserLogin();
}
}
} catch (Exception e) {
e.printStackTrace();
}
然后我将ArrayList传递给第二个Activity
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
if (userOK == true){
Intent DisplayValidList = new Intent(MainActivity.this, DisplayValidList.class);
DisplayValidList.putExtra("Capabilities", listofCapabilities);
startActivity(DisplayValidList);
}else {
Toast.makeText(MainActivity.this,
"You have entered an invalid User Name or Password. Please try again.", Toast.LENGTH_LONG).show();
}
}
这是我在第二项活动中获得的内容
ArrayList userCapabilities;
userCapabilities = getIntent().getStringArrayListExtra("Capabilities");
userCapabilities = {ArrayList@4448} size = 2
0 = {UserLogin@4458}
objMobileCapabilityDesc = "Test Rick"
objMobileCapabilityId = 0
objMobileCapabilityValue = 0
objMobileDisplayOrder = 0
shadow$_klass_ = {Class@4033} "class rick.damselfly.com.traceall_mobile_app.UserLogin"
shadow$_monitor_ = -1993118517
1 = {UserLogin@4459}
objMobileCapabilityDesc = "Receive RM (Add/Edit/Delete)"
objMobileCapabilityId = 0
objMobileCapabilityValue = 0
objMobileDisplayOrder = 0
shadow$_klass_ = {Class@4033} "class rick.damselfly.com.traceall_mobile_app.UserLogin"
shadow$_monitor_ = -2045944664
很抱歉代码的数量,不知道描述我想要做的最好的方法。我做错了什么?
答案 0 :(得分:0)
好吧,基本上你没有正确地创建你的对象。请看下面的内容。
首先是你的班级。除非您以不同的方式定义变量,否则您将无法检索这些值。为了使示例更容易,我只是在您的值中添加了一些getter。
public class UserLogin {
public int objMobileCapabilityId;
public int objMobileCapabilityValue;
public String objMobileCapabilityDesc;
public int objMobileDisplayOrder;
public UserLogin(){}
public UserLogin(int objmobilecapabilityid, int objmobilecapabilityvalue, String objmobilecapabilitydesc, int objmobiledisplayorder) {
objMobileCapabilityId = objmobilecapabilityid;
objMobileCapabilityValue = objmobilecapabilityvalue;
objMobileCapabilityDesc = objmobilecapabilitydesc;
objMobileDisplayOrder = objmobiledisplayorder;
}
public int getObjMobileCapabilityId() {
return objMobileCapabilityId;
}
public int getObjMobileCapabilityValue() {
return objMobileCapabilityValue;
}
public String getObjMobileCapabilityDesc() {
return objMobileCapabilityDesc;
}
public int getObjMobileDisplayOrder() {
return objMobileDisplayOrder;
}
}
接下来,可能更大的问题是您检索和解析JSON以创建新用户对象的方式。下面你可以看到我是如何做到这一点的。基本上你可以创建JSON数组,然后在for循环中创建单独的JSONObject,然后可以访问它们来创建UserLogin对象。另外你会看到我创建了一个String来保存JSON,这只是你的响应字符串(我这样做是为了便于测试)。
private void readYourJSON(){
String jsonString = "[{\"objMobileCapabilityId\":2,\"objMobileCapabilityValue\":2,\"objMobileCapabilityDe\":\"Test Rick\",\"objMobileDisplayOrder\":2},{\"objMobileCapabilityId\":1,\"objMobileCapability\":1,\"objMobileCapabilityDesc\":\"Receive RM (Add\\/Edit\\/Delete)\",\"objMobileDisplayOrder\":1}]";
ArrayList<UserLogin> users = new ArrayList<>();
try {
JSONArray array = new JSONArray(jsonString);
Log.e("Size: ", String.valueOf(array.length()));
for(int i = 0; i < array.length(); i++){
JSONObject currentObject = (JSONObject) array.get(i);
users.add(new UserLogin(currentObject.getInt("objMobileCapabilityId"),
currentObject.getInt("objMobileCapabilityValue"),
currentObject.getString("objMobileCapabilityDe"),
currentObject.getInt("objMobileDisplayOrder")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
txt.setText(users.get(0).getObjMobileCapabilityDesc());
}
现在您可以忽略底部的txt.setText,这样我就可以向您展示更改的最终结果。我将JSON解析卡入了一个小方法,所以这很明显这是出于示例目的,您需要根据自己的需要进行定制。另外,我强烈建议您查看下面发布的资源,以帮助您快速了解此类内容。
最终结果示例
<强>资源强>
JSON - http://www.androidhive.info/2012/01/android-json-parsing-tutorial/