我试图在android中使用SOAP解析来自Web服务的JSON数据。我的数据显示在调试器中,但不在应用程序中。我正在使用SOAP,因为在我的WebService中,JSON数据是XML标记。当我在MainActivity中调用JSONOBJECT时会抛出异常&不要输入for循环。它通常会给我一个错误,即字符串无法转换为JSONOBJECT或整数无法转换为JSONOBJECT。我想在列表中显示我正在使用另一个类 TableAdapter 的数据,并且我还使用名为表的模型,其中getter&调用setter函数。告诉我该怎么做。
public class MainActivity extends AppCompatActivity implements MyInterface
{
ListView List;
TableAdapter adapter;
ArrayList<Table> tableArrayList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List = (ListView) findViewById(R.id.list);
tableArrayList = new ArrayList<Table>();
String SOAP_ACTION = "http://tempuri.org/xxxx";
String OPERATION_NAME = "xxxx";
WebServiceHandler webServiceHandler = new WebServiceHandler(MainActivity.this);
webServiceHandler.setValues(SOAP_ACTION, OPERATION_NAME);
try
{
webServiceHandler.executeProcedure();
}
catch (Exception e)
{
Toast.makeText(this, "cant connect", Toast.LENGTH_SHORT).show();
}
}
@Override
public void StartedRequest()
{
}
@Override
public void CodeFinished(String methodName, Object Data) {
// String str = Data.toString();
// if (str != null)
{
try
{
JSONObject jsonObject = new JSONObject(String.valueOf(data));
JSONArray jsonArray = jsonObject.getJSONArray("Table");
for( int i=0; i < jsonArray.length(); i++ )
{
Table table = new Table();
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
table.setRequestID(jsonObject1.getInt("Request_ID"));
table.setOwnerID(jsonObject1.getInt("Owner_ID"));
table.setCustomerID(jsonObject1.getInt("Customer_ID"));
table.setAssetName(jsonObject1.getString("AssetName"));
table.setAddress(jsonObject1.getString("Address"));
table.setDateTime(jsonObject1.getString("DateTime"));
table.setOwnerName(jsonObject1.getString("OwnerName"));
table.setCustomerName(jsonObject1.getString("CustomerName"));
tableArrayList.add(table);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
TableAdapter adapter = new TableAdapter(getApplicationContext(), R.layout.row, tableArrayList);
List.setAdapter(adapter);
}
}
@Override
public void CodeFinishedWithException(Exception ex, String exp)
{
}
@Override
public void CodeEndedRequest()
{
}
}
我面临着问题。调试器不要输入此for循环。
@Override
public void CodeFinished(String methodName, Object Data) {
// String str = Data.toString();
// if (str != null)
{
try
{
JSONObject jsonObject = new JSONObject(String.valueOf(data));
JSONArray jsonArray = jsonObject.getJSONArray("Table");
for( int i=0; i < jsonArray.length(); i++ )
{
Table table = new Table();
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
table.setRequestID(jsonObject1.getInt("Request_ID"));
table.setOwnerID(jsonObject1.getInt("Owner_ID"));
table.setCustomerID(jsonObject1.getInt("Customer_ID"));
table.setAssetName(jsonObject1.getString("AssetName"));
table.setAddress(jsonObject1.getString("Address"));
table.setDateTime(jsonObject1.getString("DateTime"));
table.setOwnerName(jsonObject1.getString("OwnerName"));
table.setCustomerName(jsonObject1.getString("CustomerName"));
tableArrayList.add(table);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
TableAdapter adapter = new TableAdapter(getApplicationContext(), R.layout.row, tableArrayList);
List.setAdapter(adapter);
}
<string xmlns="http://tempuri.org/">
{ "Table": [ { "Request_ID": 2112, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/24/2016 1:18:44 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2113, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/24/2016 1:45:00 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2121, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/26/2016 3:57:45 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2124, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/26/2016 4:43:19 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2125, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/26/2016 4:50:39 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2126, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/27/2016 1:19:32 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2127, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/27/2016 1:27:36 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2128, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/27/2016 1:35:38 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" }, { "Request_ID": 2129, "Owner_ID": 1668, "Customer_ID": 1476, "AssetName": "Two Bed Room House", "Address": "xyz", "DateTime": "12/27/2016 1:38:08 PM", "OwnerName": "IAmAOwner", "OwnerNumber": "+923363636013", "CustomerName": "arham" } ] }
</string>
答案 0 :(得分:0)
public void CodeFinished(String methodName, Object Data)
此处数据不是字符串对象。这可能会导致问题。 这可以是普通的java对象{可能是pojo}类,你不能将它转换为字符串。
答案 1 :(得分:0)
试试这个:
SoapPrimitive resultSoapPrimitive;
resultSoapPrimitive = (SoapPrimitive) Data.getResponse();
if (resultSoapPrimitive != null) {
result = resultSoapPrimitive.toString();
}