我使用使用此公共 Northwind 数据服务的 OData4J 开发 Android 应用程序
services.odata.org/northwind/northwind.svc/
现在我收到了这个错误
URI 中指定的键数与资源 NorthwindModel.Order_Details 的键属性数不匹配
这行代码发生了:
OEntity productALFKI =c.getEntity("Order_Details",productID).execute();
productID 的定义是:
int productID= (Integer) entity2.getProperty("ProductID").getValue();
这是 MainActivity.java 类的所有代码:
package com.example.androwcf1;
import java.util.ArrayList;
import java.util.List;
import org.core4j.Enumerable;
import org.odata4j.consumer.ODataConsumer;
import org.odata4j.core.OEntity;
import org.odata4j.jersey.consumer.ODataJerseyConsumer;
import org.core4j.Enumerable;
import org.odata4j.consumer.ODataConsumer;
import org.odata4j.core.OEntity;
import org.odata4j.core.ORelatedEntitiesLink;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
static int length;
static List<OEntity> listEntities1,listEntities2,listEntities3 ,listEntities4,listEntities5;
Button call;
ListView list;
static ArrayList<String> products,categories;
static List<String> customers1Title,products1Title,order_details1Title,customers10,customers11,products10,order_details10,order_details11,orders10,orders11,arr,returned_arr,returned_arr_Title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("WCF Example");
call = (Button) findViewById (R.id.mybtn);
list = (ListView) findViewById(R.id.mylistview);
products = new ArrayList<String>();
categories = new ArrayList<String>();
customers1Title = new ArrayList <String>();
products1Title = new ArrayList <String>();
order_details1Title = new ArrayList <String>();
customers10 = new ArrayList <String>();
customers11 = new ArrayList <String>();
products10 = new ArrayList <String>();
order_details10 = new ArrayList <String>();
order_details11= new ArrayList <String>();
orders10 = new ArrayList <String>();
orders11 = new ArrayList <String>();
arr = new ArrayList <String>();
returned_arr= new ArrayList <String>();
returned_arr_Title= new ArrayList <String>();
call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto- generated method stub
new callService().execute();
}
});
}
public class callService extends AsyncTask<Void, Void, List<String>> {
@Override
protected List<String> doInBackground(Void... params) {
/////////////////////////////////
String[] strArray ;
String strName0,strName1,strName2,strName3,strName4,strName5,strName6;
String strName0o,strName1o,strName2o ;
String strName00 = "CustomerID";
String strName01 = "CompanyName";
String strName02 = "ProductName";
String strName03 = "OrderID";
String strName04 = "EmployeeID";
String strName05 = "UnitPrice";
String strName06 = "Quantity";
returned_arr_Title.add(strName00);
returned_arr_Title.add(strName01);
returned_arr_Title.add(strName02);
returned_arr_Title.add(strName03);
returned_arr_Title.add(strName04);
returned_arr_Title.add(strName05);
returned_arr_Title.add(strName06);
////////////////////////////////////////////////////////////////////////
ODataConsumer c = ODataJerseyConsumer
.create("http://services.odata.org/Northwind/Northwind.svc");
OEntity customerALFKI =c.getEntity("Customers", "ALFKI").execute();
listEntities1 = c.getEntities("Customers").filter("CustomerID eq 'ALFKI'").select("CustomerID,CompanyName").execute().toList();
listEntities3 = c.getEntities(customerALFKI.getLink("Orders",ORelatedEntitiesLink.class)).select("OrderID,EmployeeID").execute().toList();
//listEntities5 = c.getEntities(customerALFKI.getLink("Orders",ORelatedEntitiesLink.class)).select("OrderID,EmployeeID").execute().toList();
strName0o = listEntities1.get(0).getProperty("CustomerID").getValue().toString();
strName1o = listEntities1.get(0).getProperty("CompanyName").getValue().toString();
///////////////////////////////
if (listEntities3.size() > 0) {
for (OEntity entity : listEntities3) {
int oredrID= (Integer) entity.getProperty("OrderID").getValue();
strName0 = entity.getProperty("OrderID").getValue().toString();
strName1 = entity.getProperty("EmployeeID").getValue().toString();
OEntity orderALFKI =c.getEntity("Orders", oredrID).execute();
listEntities4 = c.getEntities(orderALFKI.getLink("Order_Details",ORelatedEntitiesLink.class)).select("ProductID,UnitPrice,Quantity").execute().toList();
if (listEntities4.size() > 0) {
for (OEntity entity2 : listEntities4) {
int productID= (Integer) entity2.getProperty("ProductID").getValue();
strName2 = entity2.getProperty("ProductID").getValue().toString();
strName3 = entity2.getProperty("UnitPrice").getValue().toString();
strName4 = entity2.getProperty("Quantity").getValue().toString();
customers10.add(strName0o);
customers11.add(strName1o);
orders10.add( strName0);
orders11.add( strName1);
order_details10.add( strName3);
order_details11.add( strName4);
OEntity productALFKI =c.getEntity("Order_Details", productID).execute();
// listEntities5 = c.getEntities(productALFKI.getLink("Products",ORelatedEntitiesLink.class)).select("ProductName").execute().toList();
/*
if (listEntities5.size() > 0) {
for (OEntity entity3 : listEntities5) {
strName5 = entity3.getProperty("ProductName").getValue().toString();
customers10.add(strName0o);
customers11.add(strName1o);
orders10.add( strName0);
orders11.add( strName1);
order_details10.add( strName3);
order_details11.add( strName4);
products10.add( strName5);
}
}
*/
}
}
}
}
arr.addAll( customers10);
arr.addAll(customers11);
arr.addAll( orders10);
arr.addAll( orders11);
// arr.addAll(products10);
arr.addAll(order_details10);
arr.addAll(order_details11);
for(int k3=0; k3<customers10.size(); k3++)
{
returned_arr.add(customers10.get(k3));
returned_arr.add(customers11.get(k3));
// returned_arr.add(products10.get(k3));
returned_arr.add(orders10.get(k3));
returned_arr.add(orders11.get(k3));
returned_arr.add(order_details10.get(k3));
returned_arr.add(order_details11.get(k3));
///////////////////////////////////
}
// System.out.println("RESULT="+customers10.size());
return arr;
}
@Override
protected void onPostExecute(List<String> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Intent i=new Intent(getApplicationContext(), Table1.class);
startActivity(i);
}
}
}