【问题标题】:How can I parse JSON in android using SOAP?如何使用 SOAP 在 android 中解析 JSON?
【发布时间】:2016-12-29 11:57:11
【问题描述】:

我正在尝试使用 Android 中的 SOAP 解析来自 Web 服务的 JSON 数据。我的数据显示在调试器中,但不在应用程序中。我正在使用 SOAP,因为在我的 WebService JSON 数据中是 XML 标记。当我在 MainActivity 中调用 JSONOBJECT 时,它会引发异常并且不要进入 for 循环。它通常会给我错误,即字符串无法转换为 JSONOBJECT 或整数无法转换为 JSONOBJECT。我想在一个列表中显示所有数据,我正在使用另一个类 TableAdapter 并且我还使用一个名为 Table 的模型,其中调用了 getter 和 setter 函数。告诉我该怎么做。

这是我的 MainActivity

​​>
 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>

【问题讨论】:

  • 发布您遇到异常的代码以及错误日志。
  • @MonishKamble 我更新了我的问题,我的错误日志中没有任何错误或其他任何内容。
  • 这里的data 是什么?
  • 它的数据不是数据我一定是弄错了!
  • 它在 Codefinished & using as a object 方法中

标签: android json web-services soap


【解决方案1】:
public void CodeFinished(String methodName, Object Data) 

这里的数据不是字符串对象。这可能会导致问题。 这可以是一个普通的 java 对象 {可能是 pojo} 类,您不能将其转换为字符串。

【讨论】:

  • 我该怎么办?你能告诉吗??
  • 请调试看看是什么响应
【解决方案2】:

试试这个:

SoapPrimitive resultSoapPrimitive;
resultSoapPrimitive = (SoapPrimitive) Data.getResponse();
if (resultSoapPrimitive != null) {
    result = resultSoapPrimitive.toString();
}

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 2021-09-06
    • 1970-01-01
    • 2012-05-29
    相关资源
    最近更新 更多