2013-03-14 117 views
1

我创建了两个Java文件,一个是Main.java和Category.javaAndroid的Web服务的XML解析

我加入库文件ksoap2-Android的组装2.6.2-JAR-与-dependencies.jar

但是这里输出不显示。

我正在做soap xml解析。

在这里文本视图,但我想要一个列表。请帮我

Main.java

public class Main extends Activity { 

private static String SOAP_ACTION = "http://tempuri.org/HelloWorld"; 

private static String NAMESPACE = "http://tempuri.org/"; 
private static String METHOD_NAME = "HelloWorld"; 

private static String URL = "http://sygnetinfosol.com/webservice.asmx"; 



/** Called when the activity is first created. */ 
@SuppressWarnings("static-access") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //Initialize soap request + add parameters 
//  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);   

    SoapObject request = GetSoapObject(METHOD_NAME); 
    SoapSerializationEnvelope envelope = GetEnvelope(request); 


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    envelope.addMapping(NAMESPACE, "Category",new Category().getClass()); 

    try { 
    androidHttpTransport.call(SOAP_ACTION, envelope); 

    SoapObject response = (SoapObject) envelope.getResponse(); 
    Category[] Allrooms = RetrieveFromSoap(response); 

    TextView actv = (TextView)findViewById(R.id.resultbox); 
    actv.setHint(Allrooms[0].Text1); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 
    public static SoapObject GetSoapObject(String MethodName) 
    { 
    return new SoapObject(NAMESPACE,MethodName); 
    } 
    public static SoapSerializationEnvelope GetEnvelope(SoapObject Soap) 
    { 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(Soap); 
    return envelope; 
    } 

    public static Category[] RetrieveFromSoap(SoapObject soap) 
    { 
     Category[] categories = new Category[soap.getPropertyCount()]; 
    for (int i = 0; i < categories.length; i++) { 
    SoapObject pii = (SoapObject)soap.getProperty(i); 
    Category category = new Category(); 
    Category.Text1 = pii.getProperty(0).toString(); 
    Category.Text2= pii.getProperty(1).toString(); 
    Category.Text3= pii.getProperty(2).toString(); 
    categories[i] = category; 
    } 
    return categories; 
    } 
    } 

Category.java

public class Category implements KvmSerializable{  

public static String Text1; 
public static String Text2; 
public static String Text3; 

public Category(){} 


public Category(String lat, String longi, String address) { 

    Text1 = lat; 
    Text2 = longi; 
    Text3 = address; 
} 


public Object getProperty(int arg0) { 

    switch(arg0) 
    { 
    case 0: 
     return Text1; 
    case 1: 
     return Text2; 
    case 2: 
     return Text3; 
    } 

    return null; 
} 

public int getPropertyCount() { 
    return 3; 
} 

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
    switch(index) 
    { 
    case 0: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Text1"; 
     break; 
    case 1: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Text2"; 
     break; 
    case 2: 
     info.type = PropertyInfo.STRING_CLASS; 
     info.name = "Text3"; 
     break; 
    default:break; 
    } 
} 

public void setProperty(int index, Object value) { 
    switch(index) 
    { 
    case 0: 
     Text1 = value.toString(); 
     break; 
    case 1: 
     Text2 = value.toString(); 
     break; 
    case 2: 
     Text3 = value.toString(); 
     break; 
    default: 
     break; 
    } 
} 
} 

logcat的

03-14 11:59:05.417: W/PackageManager(59): Code path for pkg : com.pxr.tutorial.soap.weather changing from /data/app/com.pxr.tutorial.soap.weather-2.apk to /data/app/com.pxr.tutorial.soap.weather-1.apk 
03-14 11:59:05.417: W/PackageManager(59): Resource path for pkg : com.pxr.tutorial.soap.weather changing from /data/app/com.pxr.tutorial.soap.weather-2.apk to /data/app/com.pxr.tutorial.soap.weather-1.apk 
03-14 11:59:05.422: D/PackageManager(59): Activities: com.pxr.tutorial.soap.weather.Main 
03-14 11:59:05.462: I/ActivityManager(59): Force stopping package com.pxr.tutorial.soap.weather uid=10039 
03-14 11:59:06.276: I/installd(35): move /data/dalvik-cache/[email protected]@[email protected] -> /data/dalvik- cache/[email protected]@[email protected] 
03-14 11:59:06.282: D/PackageManager(59): New package installed in /data/app/com.pxr.tutorial.soap.weather-1.apk 
03-14 11:59:07.162: D/dalvikvm(257): GC_EXPLICIT freed 154 objects/11496 bytes in 87ms 
03-14 11:59:07.192: I/ActivityManager(59): Force stopping package com.pxr.tutorial.soap.weather uid=10039 
03-14 11:59:07.282: D/dalvikvm(119): GC_EXPLICIT freed 791 objects/44936 bytes in 85ms 
03-14 11:59:07.392: D/dalvikvm(59): GC_EXPLICIT freed 9323 objects/576216 bytes in 149ms 
03-14 11:59:07.662: W/RecognitionManagerService(59): no available voice recognition services found 
03-14 11:59:07.872: D/dalvikvm(157): GC_EXPLICIT freed 1881 objects/96152 bytes in 421ms 
03-14 11:59:07.982: D/dalvikvm(59): GC_EXPLICIT freed 4037 objects/225296 bytes in 105ms 
03-14 11:59:08.042: I/installd(35): unlink /data/dalvik-cache/[email protected]@[email protected] 
03-14 11:59:08.102: D/AndroidRuntime(575): Shutting down VM 
03-14 11:59:08.112: D/dalvikvm(575): Debugger has detached; object registry had 1 entries 
03-14 11:59:08.702: D/AndroidRuntime(588): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 
03-14 11:59:08.702: D/AndroidRuntime(588): CheckJNI is ON 
03-14 11:59:08.852: D/AndroidRuntime(588): --- registering native functions --- 
03-14 11:59:09.572: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.pxr.tutorial.soap.weather/.Main } 
03-14 11:59:09.912: I/ActivityManager(59): Start proc com.pxr.tutorial.soap.weather for activity com.pxr.tutorial.soap.weather/.Main: pid=594 uid=10039 gids={3003} 
03-14 11:59:09.922: D/AndroidRuntime(588): Shutting down VM 
03-14 11:59:09.922: D/jdwp(588): Got wake-up signal, bailing out of select 
03-14 11:59:09.922: D/dalvikvm(588): Debugger has detached; object registry had 1 entries 
03-14 11:59:09.952: I/dalvikvm(588): JNI: AttachCurrentThread (from ???.???) 
03-14 11:59:09.952: I/AndroidRuntime(588): NOTE: attach of thread 'Binder Thread #3' failed 
03-14 11:59:11.171: I/ActivityManager(59): Displayed activity com.pxr.tutorial.soap.weather/.Main: 1269 ms (total 1269 ms) 
03-14 11:59:16.412: D/dalvikvm(119): GC_EXPLICIT freed 728 objects/42320 bytes in 158ms 
03-14 11:59:21.422: D/dalvikvm(265): GC_EXPLICIT freed 109 objects/5360 bytes in 150ms 
+0

有你添加的权限'INTERNET ACCESS'在你的清单文件。 – Prateek 2013-03-14 06:18:28

+0

@pKs是的我加入 – Satya 2013-03-14 06:19:18

+0

你能否提供给我们日志猫的信息。 – 2013-03-14 06:26:46

回答

1
ok I will send you all the classes and xml files. check and learn the web services concept.  


LazyAdapter.java 


     public class LazyAdapter extends BaseAdapter { 
      java.net.URL urlImage; 
      InputStream is ; 
      private Activity activity; 
      private ArrayList<HashMap<String, String>> twitterList; 
      private static LayoutInflater inflater=null; 

      public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
       activity = a; 
       twitterList=d; 
       inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      } 

      public int getCount() { 
       return twitterList.size(); 
      } 

      public Object getItem(int position) { 
       return position; 
      } 

      public long getItemId(int position) { 
       return position; 
      } 

      public View getView(int position, View convertView, ViewGroup parent) { 
       View vi=convertView; 
       if(convertView==null) 
        vi = inflater.inflate(R.layout.list_item, null); 

       TextView id = (TextView)vi.findViewById(R.id.playerId); // title 
       TextView name = (TextView)vi.findViewById(R.id.playerName); // artist name 
       TextView country = (TextView)vi.findViewById(R.id.countryName); // duration 
       ImageView flag=(ImageView)vi.findViewById(R.id.countryFlag); // thumb image 


       HashMap<String, String> valueList = new HashMap<String, String>(); 
       valueList = twitterList.get(position); 


       // Setting all values in listview 
       id.setText(valueList.get(WSDemoFootBallActivity.TAG_ID)); 
       name.setText(valueList.get(WSDemoFootBallActivity.TAG_NAME)); 
       country.setText(valueList.get(WSDemoFootBallActivity.TAG_COUNTRYNAME)); 
       String countryFlag = valueList.get(WSDemoFootBallActivity.TAG_COUNTRYFLAG); 
       try { 
        urlImage = new java.net.URL(countryFlag); 
        is = (InputStream) urlImage.getContent(); 


       } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       Drawable d = Drawable.createFromStream(is, null); 
       flag.setImageDrawable(d); 
       return vi; 
      } 
     } 


    SingleMenuItemActivity.java 



    public class SingleMenuItemActivity extends Activity { 

     URL url; 
     Bitmap bimage; 
     java.net.URL urlImage; 
     InputStream is ; 
     private static final String TAG_ID = "id"; 
     private static final String TAG_NAME = "name"; 
     private static final String TAG_COUNTRYNAME = "countryName"; 
     private static final String TAG_COUNTRYFLAG = "countryFlag"; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.single_list_item); 

      // getting intent data 
      Intent in = getIntent(); 

      // Get JSON values from previous intent 
      String playerId = in.getStringExtra(TAG_ID); 
      String playerName = in.getStringExtra(TAG_NAME); 
      String countryName = in.getStringExtra(TAG_COUNTRYNAME); 
      String countryFlag = in.getStringExtra(TAG_COUNTRYFLAG); 



      // Displaying all values on the screen 
      TextView lblId = (TextView) findViewById(R.id.playerId); 
      TextView lblName = (TextView) findViewById(R.id.playerName); 
      TextView lblCountryName = (TextView) findViewById(R.id.countryName); 
      ImageView lblCountryFlag = (ImageView) findViewById(R.id.countryFlag); 
      try { 
       urlImage = new java.net.URL(countryFlag); 
       is = (InputStream) urlImage.getContent(); 


      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Drawable d = Drawable.createFromStream(is, null); 
      lblId.setText(playerId); 
      lblName.setText(playerName); 
      lblCountryName.setText(countryName); 
      lblCountryFlag.setImageDrawable(d); 

     } 

    } 


    main.xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 
     <!-- Main ListView 
      Always give id value as list(@android:id/list) 
     --> 
     <ListView 
      android:id="@+id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 


    </LinearLayout> 



    list_item.xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:padding="10dp" > 
     <ImageView 
      android:id="@+id/countryFlag" 
      android:layout_width="70dp" 
      android:layout_height="50dp" 
      android:gravity="left" 
      android:src="@drawable/ic_launcher" > 
     </ImageView> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:orientation="vertical" > 

      <TextView 
       android:id="@+id/playerId" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="2dip" 
       android:paddingTop="6dip" 
       android:textColor="#43bd00" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/playerName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="2dip" 
       android:textColor="#acacac" > 
      </TextView> 

      <TextView 
       android:id="@+id/countryName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:textColor="#5d5d5d" > 
      </TextView> 
     </LinearLayout> 

    </LinearLayout> 



    single_list_item.xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="2dip" 
       android:paddingTop="6dip" 
       android:text="PlayerId :" 
       android:textColor="#43bd00" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/playerId" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="2dip" 
       android:paddingTop="6dip" 
       android:textColor="#43bd00" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="2dip" 
       android:text="PlayerName :" 
       android:textColor="#acacac" 
       android:textSize="16sp" > 
      </TextView> 

      <TextView 
       android:id="@+id/playerName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingBottom="2dip" 
       android:textColor="#acacac" > 
      </TextView> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:text="CountryName :" 
       android:textColor="#5d5d5d" > 
      </TextView> 

      <TextView 
       android:id="@+id/countryName" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:textColor="#5d5d5d" > 
      </TextView> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:text="CountryFlag :" 
       android:textColor="#43bd00" > 
      </TextView> 

      <ImageView 
       android:id="@+id/countryFlag" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="left" > 
      </ImageView> 
     </LinearLayout> 

    </LinearLayout> 
0

见检索使用SOAP.It Web服务数据下面的示例代码会帮助你很多。让我知道你有任何疑问。

import java.io.IOException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import org.xmlpull.v1.XmlPullParserException; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.TextView; 

public class WSDemoFootBallActivity extends Activity { 

SoapObject resultinMethod; 
TextView tvResult; 
URL url; 
Bitmap bimage; 
ListView list; 
LazyAdapter adapter; 
static final String TAG_ID = "id"; 
static final String TAG_NAME = "name"; 
static final String TAG_COUNTRYNAME = "countryName"; 
static final String TAG_COUNTRYFLAG = "countryFlag"; 

private static final String METHOD_NAME = "AllPlayerNames"; 
private static final String SOAP_ACTION = ""; 
private static final String NAMESPACE = "http://footballpool.dataaccess.eu"; 
private static final String URL = "http://footballpool.dataaccess.eu/data/info.wso?WSDL&quot"; 
//you can get these values from the wsdl file^ 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME); 
    request.addProperty("bSelected", true); 

    SoapSerializationEnvelope sse=new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11); 
    sse.setOutputSoapObject(request); 
    sse.dotNet=true; 

    HttpTransportSE htse=new HttpTransportSE(URL); 
    try { 
     htse.call(SOAP_ACTION, sse); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    SoapObject res=(SoapObject) sse.bodyIn; 

    final ArrayList<HashMap<String, String>> valuesList = new ArrayList<HashMap<String, String>>(); 

    for (int i = 0; i < res.getPropertyCount(); i++) { 

     SoapObject namesObject = (SoapObject) res.getProperty(i); 
     for(int j=0;j<namesObject.getPropertyCount();j++) 
     { 
      Object objectNames=namesObject.getProperty(j); 
      SoapObject soapObjectIds = (SoapObject)objectNames; 
      SoapObject soapObjectNames=(SoapObject) objectNames; 
      SoapObject soapObjectCountryNames = (SoapObject)objectNames; 
      SoapObject soapObjectCountryFlag = (SoapObject)objectNames; 

      String id = soapObjectIds.getProperty("iId").toString(); 
      String stringNames=soapObjectNames.getProperty("sName").toString(); 
      String countryName = soapObjectCountryNames.getProperty("sCountryName").toString(); 
      String countryFlag = soapObjectCountryFlag.getProperty("sCountryFlagLarge").toString();  


      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_NAME, stringNames); 
      map.put(TAG_COUNTRYNAME, countryName); 
      map.put(TAG_COUNTRYFLAG, countryFlag); 

      // adding HashList to ArrayList 
      valuesList.add(map); 
     } 
    } 
    /** 
    * Updating parsed JSON data into ListView 
    * */ 


    // selecting single ListView item 
    ListView list = (ListView) findViewById(R.id.list); 

    // Getting adapter by passing xml data ArrayList 
    adapter=new LazyAdapter(this, valuesList);   
    list.setAdapter(adapter); 
    // Launching new screen on Selecting Single ListItem 
    list.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      HashMap<String, String> map = new HashMap<String, String>(); 
       map = valuesList.get(position); 
      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class); 
      in.putExtra(TAG_ID,map.get(WSDemoFootBallActivity.TAG_ID)); 
      in.putExtra(TAG_NAME,map.get(WSDemoFootBallActivity.TAG_NAME)); 
      in.putExtra(TAG_COUNTRYNAME, map.get(WSDemoFootBallActivity.TAG_COUNTRYNAME)); 
      in.putExtra(TAG_COUNTRYFLAG,map.get(WSDemoFootBallActivity.TAG_COUNTRYFLAG)); 
      startActivity(in); 

     } 
    }); 
} 
} 
+0

遵循此代码并清理一次项目并运行它。 – 2013-03-14 06:39:51

+0

请给我一个LazyAdapter.java和SingleMenuItemActivity.java – Satya 2013-03-14 06:54:36

+0

请帮我LazyAdapter – Satya 2013-03-14 07:00:31