以下は、 JSON文字列を返そうとするasp Webサービスの出力です(JSON文字列を返しているかどうかはわかりません)。
Androidアプリケーションがこれを消費した後、文字列は次のようになります。
GetCustomerListResponse {GetCustomerListResult = [{"VehicleID": "KL-9876"、 "VehicleType": "Nissan"、 "VehicleOwner": "Sanjiva"}];}
[これはjson文字列ではないことをかなり確信しています]。
androidプログラムがjson文字列を消費するように変更する必要があることを知りたいです。
よろしくお願いします。私の完全なaspxコードとandroidコードを以下に示します。
Androidコード:
package com.example.objectpass;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;
public class MainActivity extends Activity {
TextView resultA;
Spinner spinnerC;
@Override
public void onCreate(Bundle savedInstanceState) {
String[] toSpinnerSum;
toSpinnerSum = new String[9];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerC = (Spinner) findViewById(R.id.spinner1);
resultA = (TextView) findViewById(R.id.textView2);
final String NAMESPACE = "http://tempuri.org/";
final String METHOD_NAME = "GetCustomerList";
final String SOAP_ACTION = "http://tempuri.org/GetCustomerList";
final String URL = "http://192.168.1.100/WebService4/Service1.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try {
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject response = (SoapObject) soapEnvelope.bodyIn;
JSONArray jArray = new JSONArray();
resultA.setText(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
ASP WEBサービスコード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
namespace WebService4
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCustomerList()
{
Vehicle vehi = simpleCase();
List<Vehicle> newL = new List<Vehicle> { vehi };
string output = JsonConvert.SerializeObject(newL);
// return newL;
return output;
}
[WebMethod]
public Vehicle simpleCase()
{
Vehicle obj = new Vehicle();
obj.VehicleID = "KL-9876";
obj.VehicleType = "Nissan";
obj.VehicleOwner = "Sanjiva";
return obj;
}
}
public class Vehicle
{
public string VehicleID { get; set; }
public string VehicleType { get; set; }
public string VehicleOwner { get; set; }
}
}