(Android)我在使用Web服务的代码中得到一个ClassCastException
我在Logcat的这一行得到(Android)我在使用Web服务的代码中得到一个ClassCastException,android,web-services,adapter,classcastexception,Android,Web Services,Adapter,Classcastexception,我在Logcat的这一行得到ClassCastException:Standort-Standort=(Standort)响应 根据该代码: private void loadLocationMenuList() { ServiceProxy.createWebServiceTask(getActivity(), new RemoteCallListener() { @Override public void onRemoteCallError(
ClassCastException
:Standort-Standort=(Standort)响应代码>
根据该代码:
private void loadLocationMenuList() {
ServiceProxy.createWebServiceTask(getActivity(), new RemoteCallListener() {
@Override
public void onRemoteCallError(String response) {
// TODO Auto-generated method stub
}
@Override
public void onRemoteCallComplete(Object response) {
Log.d("debug", "response is = "+response+"\t"+response.getClass());
Standort standort = (Standort) response;
locationMenuAdapter = new LocationMenuAdapter(StandorteFragment.this, standort);
menuItemListLoc.setAdapter(locationMenuAdapter);
}
@Override
public void onNoInternetError() {
// TODO Auto-generated method stub
}
@Override
public void onNoAccess() {
// TODO Auto-generated method stub
}
}, true).invokeGetStandorte();
}
服务代理:
public class ServiceProxy {
private static final boolean useFakeService = true;
public static IWebServiceTask createWebServiceTask(Context ctx, RemoteCallListener listener, boolean hasInternet) {
//if (useFakeService) {
return new FakeService(ctx, listener);
}
}
伪造服务:
public class FakeService extends CallWebServiceTask {
private final boolean hasInternet = true;
public FakeService(Context ctx, RemoteCallListener listener) {
super(ctx, listener);
}
@Override
protected WebEntityInterface doInBackground(CallWebServiceTask.InputParam... params) {
System.currentTimeMillis();
WebEntityInterface result = null;
CallWebServiceTask.InputParam param = null;
param = params[0];
if (param.url.startsWith(Constants.invokeGetStandorte)) {
return super.doInBackground(params);
}
try {
if (hasInternet) {
if (param.CALL_TYPE == InputParam.GET) {
result = sendGET(param);
} else if (param.CALL_TYPE == InputParam.POST) {
result = sendPOST(param);
}
} else {
LogService.log("CALL WEBSERVICE TASK", " NO INTERNET!!!!");
result = new WebEntity(1, "No internet connection!");
}
} catch (UnknownHostException e) {
LogService.err("WEB-SERVER-ERROR", e.getMessage(), e, LogService.LOG_LEVEL_FULL);
result = new WebEntity(1, "No internet connection!");
} catch (SocketException e) {
LogService.err("WEB-SERVER-ERROR", e.getMessage(), e, LogService.LOG_LEVEL_FULL);
result = new WebEntity(1, "No network connection!");
} catch (Throwable e) {
LogService.err("WEB-SERVER-ERROR", e.getMessage(), e, LogService.LOG_LEVEL_FULL);
result = new WebEntity(2, "Network connection error!");
}
return result;
}
@Override
public WebEntityInterface sendGET(CallWebServiceTask.InputParam param) throws Exception {
if (param.url.startsWith(Constants.invokeGetStandorte)) {
return super.sendGET(param);
}
SessionToken.Sessiontoken = "dfasmdfnasdfb";
System.out.println("-----GET[" + param.clazz + "] : " + param.url);
WebEntityInterface result = null;
Populator populator = new Populator(new RandomDriver());
result = populator.fill(param.clazz);
result.setSuccess(true);
System.out.println("------------RESULT :" + result);
return result;
}
@Override
public WebEntityInterface sendPOST(CallWebServiceTask.InputParam param) throws Exception {
if (param.url.startsWith(Constants.invokeGetStandorte)) {
return super.sendPOST(param);
}
SessionToken.Sessiontoken = "dfasmdfnasdfb";
System.out.println("-----POST : " + param.url);
WebEntityInterface result = null;
Populator populator = new Populator(new RandomDriver());
result = populator.fill(param.clazz);
result.setSuccess(true);
System.out.println("------------RESULT :" + result);
return result;
}
}
我的位置菜单适配器:
public class LocationMenuAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private AQuery aQuery;
private Standort standort;
private StandorteFragment standorteFragment;
public LocationMenuAdapter(StandorteFragment standorteFragment, Standort standort) {
this.standorteFragment =standorteFragment;
this.standort =standort;
this.aQuery = standorteFragment.aQuery;
mInflater = LayoutInflater.from(standorteFragment.getActivity());
}
public int getCount() {
return standort.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public StandortItem getEKUrl(int i) {
return standort.get(i);
}
public View getView(int position, View convertView, ViewGroup parent) {
ListContent holder;
StandortItem standortItem = getEKUrl(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listviewinflate, null);
holder = new ListContent();
holder.text = (TextView) convertView.findViewById(R.id.TextView01);
convertView.setTag(holder);
} else {
holder = (ListContent) convertView.getTag();
}
holder.text.setText(standortItem.Name);
return convertView;
}
class ListContent {
TextView text;
}
}
最后但并非最不重要的一点:WebEntity:
public class Standort extends WebEntity {
public static int IDtest;
public ArrayList<StandortItem> standort = new ArrayList<StandortItem>();
public void add(StandortItem standortItem) {
standort.add(standortItem);
}
public int size() {
return standort.size();
}
public StandortItem get(int i) {
return standort.get(i);
}
public class StandortItem implements Serializable{
public Integer ID;
public String Name;
public String Strasse;
public String Email;
public Integer PLZ;
public String Ort;
public String Telefon;
public String Fax;
public String Oeffnungszeiten;
public Boolean Bar;
public Boolean BusinessLunch;
public Boolean Parkplatz;
public Boolean Raucher;
public String GooglemapsStrasse;
public StandortItem(Integer ID, String Name,String Strasse, String Email,Integer PLZ, String Ort,String Telefon, String Fax,String Oeffnungszeiten, Boolean Bar,Boolean BusinessLunch, Boolean Parkplatz,Boolean Raucher,String GooglemapsStrasse) {
super();
this.ID = ID;
this.Name = Name;
this.Strasse = Strasse;
this.Email = Email;
this.PLZ = PLZ;
this.Ort = Ort;
this.Telefon = Telefon;
this.Fax = Fax;
this.Oeffnungszeiten = Oeffnungszeiten;
this.Bar = Bar;
this.BusinessLunch = BusinessLunch;
this.Parkplatz = Parkplatz;
this.Raucher = Raucher;
this.GooglemapsStrasse = GooglemapsStrasse;
IDtest=ID;
}
public String toString() {
return "Standort [ID=" + ID + ", Name=" + Name + ", Strasse=" + Strasse + ", Email=" + Email + ", PLZ=" + PLZ + ", Ort=" + Ort + ", Telefon=" + Telefon + ", Fax=" + Fax + ", Oeffnungszeiten=" + Oeffnungszeiten + ", Bar=" + Bar + ", BusinessLunch=" + BusinessLunch + ", Parkplatz=" + Parkplatz + ", Raucher=" + Raucher + ",GooglemapsStrasse=" + GooglemapsStrasse + "]";
}
}
}
标准运动课:
public class Standorts extends WebListEntity<Standort> {
public Standorts() {
super();
}
}
公共类标准扩展了WebListenty{
公众立场(){
超级();
}
}
PS:我在web服务方面做得不多,所以我是这方面的初学者,因此可能有部分代码编写得不太正确,如果您也能在这方面提供帮助,我将非常感激。所以我猜是这样的?我不知道你想在这里做什么真的
@Override
public void onRemoteCallComplete(Object response) {
Log.d("debug", "response is = "+response+"\t"+response.getClass());
Standorts standorts = (Standorts) response;
//to get individual Standort objects from this WebListEntity<Standort> wrapper you need
//get WebEntity from standorts
//do whatever you need to do to make a Standort object out of that.
Standort standort = new Standort();
standort.restoreFromWebEntity(theWebEntityYouGotInStepOne);
locationMenuAdapter = new LocationMenuAdapter(StandorteFragment.this, standort);
menuItemListLoc.setAdapter(locationMenuAdapter);
}
}
@覆盖
public void onRemoteCallComplete(对象响应){
Log.d(“debug”,“response is=“+response+”\t“+response.getClass());
Standorts Standorts=(Standorts)响应;
//要从这个WebListenty包装器中获取单独的标准对象,您需要
//从standorts获取WebEntity
//做任何你需要做的事情,使之成为一个标准的对象。
Standort Standort=新Standort();
标准。从WebEntity(您登录到的WebEntity)恢复;
locationMenuAdapter=新的locationMenuAdapter(StandorteFragment.this,standort);
MenuItemListLocal.setAdapter(位置MenuAdapter);
}
}
但你得到的反应肯定是立场
再看看这个WebListenty类。。。这不是您发布的WebEntity。换句话说,除了这个函数之外,您发布的所有代码都有些无用。我使用WebListenty类standers作为适配器扩展webentities(standers)的列表,以某种方式使其工作。很明显,您无法将对象响应转换为standert。试着在施法之前使用Logcat调用,找出第一个响应是什么……什么样的Logcat调用?我必须弄清楚响应是什么样子的,这样我就知道如何将其调用到Standort中:-例如,Log.d(String logtag,String logMessage)one。类似于Log.d(“debug”,“response is=“+response+”\t“+response.getClass());等查看谁调用了该方法以及传递了什么。一旦你弄清楚了输入的数据,你就会知道是你的标准还是其他什么可以帮助你重建标准或其他什么。现在我不知道如何构建我的代码,我有一个想法,认为这是合乎逻辑的(在我之后),但正如你在新编辑的LOGCAT日志中看到的,我需要一些巨大的帮助看起来你的回复是一个WebEntities的arrayList。。。但在最后,它声明它的类名为standers。所以,试着把它扔到架子上,然后把每个架子都拉出来。我要试试这个。我也不知道我在做什么。我知道我在做什么,直到我的老板给了我很多webservices实体,他说这些我们必须连接到这个项目(而不是我拥有的那些),这些实体有一个关于它们的PDF,用德语,我不懂德语,即使我知道它有些无用。这就是我在翻译中迷失的原因好吧,至少看起来你又回到了正轨上;)现在您知道在函数调用中得到了什么,问题归结为从这个WebListEntity(扩展了ArrayList?)获取每个标准项我只是想在这里画一个图表,我所看到的是一个混乱的英文/德语混合字段:D似乎有人想保住他的工作。offtopic:这似乎真的很相关。如果你在列表和适配器方面遇到问题,那么这可能也很相关,但从我看来,适配器看起来不错。10x,但它们是有效的。至少它们在Web服务出现之前工作得很好,从那以后我就再也没有改变过它们。
@Override
public void onRemoteCallComplete(Object response) {
Log.d("debug", "response is = "+response+"\t"+response.getClass());
Standorts standorts = (Standorts) response;
//to get individual Standort objects from this WebListEntity<Standort> wrapper you need
//get WebEntity from standorts
//do whatever you need to do to make a Standort object out of that.
Standort standort = new Standort();
standort.restoreFromWebEntity(theWebEntityYouGotInStepOne);
locationMenuAdapter = new LocationMenuAdapter(StandorteFragment.this, standort);
menuItemListLoc.setAdapter(locationMenuAdapter);
}
}