gpt4 book ai didi

Android web服务xml解析

转载 作者:行者123 更新时间:2023-11-30 03:43:25 24 4
gpt4 key购买 nike

我正在创建两个 java 文件,一个是 Main.java 和 Category.java

我正在添加库文件 ksoap2-android-assembly-2.6.2-jar-with-dependencies.jar

但是这里没有显示输出。

我正在做 soap XMl 解析。

这里是 TextView ,但我想要一个列表。请帮助我

主.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;
}
}

分类.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;
}
}
}

日志猫

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/data@app@com.pxr.tutorial.soap.weather-1.apk@classes.dex -> /data/dalvik- cache/data@app@com.pxr.tutorial.soap.weather-1.apk@classes.dex
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/data@app@com.pxr.tutorial.soap.weather-2.apk@classes.dex
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

最佳答案

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>

关于Android web服务xml解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com