困扰好几周了:webservice与android数据交互问题 [size=18px]传输数据(title,imgurl,account)当传输title或title+account时数据正常传输,但是无法传输Imgurl项,即使imgurl+title都一样,没有任何数据显示 请教[/size]android数据交互代码bug急请教androidwebservice 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.httpconn.javaimport java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;public class httpcon{ public ArrayList<String>GetWebServer(String methodName,ArrayList<String> Parameters,ArrayList<String> ParaValues){ ArrayList<String> Values =new ArrayList<String>(); String ServerUrl="http://10.0.2.2:58665/Service1.asmx"; String soapAction ="http://tempuri.org/" + methodName; String soap ="<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body />"; String tps, vps, ts; String mreakString = ""; mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">"; for(int i =0; i < Parameters.size(); i++) { tps = Parameters.get(i).toString(); vps = ParaValues.get(i).toString(); ts="<" + tps + ">" + vps + "</" + tps + ">"; mreakString = mreakString +ts;} mreakString = mreakString + "</" + methodName + ">"; String soap2 = "</soap:Envelope>"; String requestData = soap + mreakString + soap2; try{URL url = new URL(ServerUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); byte[] bytes = requestData.getBytes("utf-8"); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setConnectTimeout(6000);// 设置超时时间 con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "text/xml;charset=utf-8"); con.setRequestProperty("SOAPAction", soapAction); con.setRequestProperty("Content-Length", ""+bytes.length); OutputStream outStream = con.getOutputStream(); outStream.write(bytes); outStream.flush(); outStream.close(); InputStream inStream = con.getInputStream(); System.out.print("流"); Values = inputStreamtovaluelist(inStream, methodName); return Values; } catch (Exception e) { System.out.print("2221"); System.out.print("2221"); return null;}} public ArrayList<String> inputStreamtovaluelist(InputStream in, String methodName) throws IOException { StringBuffer out = new StringBuffer(); String s1 = ""; byte[] b = new byte[4096]; ArrayList<String> Values = new ArrayList<String>(); Values.clear(); for (int n; (n = in.read(b)) != -1;) { s1 = new String(b, 0, n); out.append(s1); } System.out.println(out); String[] s13 = s1.split("><"); String ifString = methodName + "Result"; String TS = ""; String vs = ""; Boolean getValueBoolean = false; for (int i = 0; i < s13.length; i++) { TS = s13[i]; System.out.println(TS); int j, k, l; j = TS.indexOf(ifString); k = TS.lastIndexOf(ifString); if (j >= 0) { System.out.println(j); if (getValueBoolean == false) { getValueBoolean = true; } else { } if ((j >= 0) && (k > j)) { System.out.println("FFF" + TS.lastIndexOf("/" + ifString)); //System.out.println(TS); l = ifString.length() + 1; vs = TS.substring(j + l, k - 2); //System.out.println("fff"+vs); Values.add(vs); System.out.println("退出" + vs); getValueBoolean = false; System.out.println("一"); return Values; } } if (TS.lastIndexOf("/" + ifString) >= 0) { getValueBoolean = false; return Values; } if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) { k = TS.length(); //System.out.println(TS); vs = TS.substring(7, k - 8); //System.out.println("f"+vs); Values.add(vs); } } System.out.println("二"); return Values; }} 2.dbUtil.javaimport java.sql.Connection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class dbUtil { private ArrayList<String> arrayList = new ArrayList<String>(); private ArrayList<String> brrayList = new ArrayList<String>(); private ArrayList<String> crrayList = new ArrayList<String>(); //private ArrayList<String> drrayList = new ArrayList<String>(); private httpcon http = new httpcon(); public List<HashMap<String, String>> chakan(String Ztz) { List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); arrayList.clear(); brrayList.clear(); crrayList.clear() ; arrayList.add("Ztz"); brrayList.add(Ztz); crrayList = http.GetWebServer("gc", arrayList, brrayList); System.out.println("22222"+crrayList); HashMap<String, String> tempHash = new HashMap<String, String>(); //tempHash.put("biaoti", "biaoti"); //list.add(tempHash); if(crrayList!=null){ for(int j = 0; j < crrayList.size(); j+=1) { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("biaoti", crrayList.get(j)); // hashMap.put("fabuzhe", crrayList.get(j+1)); // hashMap.put("url", crrayList.get(j+2)); list.add(hashMap); }} System.out.println("111111"+list); System.out.println("chakan"+list); return list; } public List<HashMap<String, String>> ejym(String Ztz) { List<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>(); arrayList.clear(); brrayList.clear(); crrayList.clear() ; arrayList.add("Ztz"); brrayList.add(Ztz); System.out.println("这是"+brrayList); crrayList = http.GetWebServer("ejym", arrayList, brrayList); System.out.println("crraylist"+crrayList); if(crrayList!=null){ for(int j = 0; j < crrayList.size(); j+=1) { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.clear(); hashMap.put("biaoti", crrayList.get(j)); // hashMap.put("url", crrayList.get(j+1)); list.add(hashMap); }} else{System.out.println("物质");} System.out.println("list"+list); return list;} public List<HashMap<String, String>>url(String Title) { List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); arrayList.clear(); brrayList.clear(); crrayList.clear() ; arrayList.add("Title"); brrayList.add(Title); System.out.println("dbutil"+brrayList); crrayList = http.GetWebServer("url", arrayList, brrayList); System.out.println("22url22"+crrayList); //HashMap<String, String> tempHash = new HashMap<String, String>(); String a=null; if(crrayList!=null){ for(int j = 0; j < crrayList.size(); j+=1) { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("content", crrayList.get(j)); // hashMap.put("url", crrayList.get(j+1)); list.add(hashMap); }} return list;} public String[] tpxw() { List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); arrayList.clear(); brrayList.clear(); crrayList.clear(); crrayList = http.GetWebServer("tpxw", arrayList, brrayList); System.out.println("图片新闻"+crrayList); String[] a=new String[5]; if(crrayList!=null){ for(int j = 0; j < crrayList.size(); j++) { a[j]="http://10.0.2.2:56041/Web"+crrayList.get(j); }} System.out.println("a值"+a); return a;}} 3.mainactivity.java主要代码 Gallery gallery=(Gallery)findViewById(R.id.gallery); try { //设置Gallery的Adapter gallery.setAdapter(new galleryadapter(this)); gallery.setSelection(0);//设置第一个图标居中 gallery.setSpacing(00);//图标之间的距离 } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace();}public class galleryadapter extends BaseAdapter { private String[] myImageURL = dbUtil_1.tpxw(); private Context mContext; public galleryadapter(Context c){ mContext=c; } @Override public int getCount() { // TODO Auto-generated method stub return myImageURL.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = new ImageView(this.mContext); try{ URL aryURI = new URL(myImageURL[position]); // 打开连接 URLConnection conn = aryURI.openConnection(); conn.connect(); // 转变为 InputStream InputStream is = conn.getInputStream(); // 将InputStream转变为Bitmap Bitmap bm = BitmapFactory.decodeStream(is); // 关闭InputStream is.close(); //添加图片 imageView.setImageBitmap(bm); } catch (IOException e) { e.printStackTrace(); } //设置布局参数 imageView.setLayoutParams(new Gallery.LayoutParams(350, 300)); return imageView; }} 4.ej.java //我去,逼我刚注册了个新号import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.lang.String.*;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.ScrollView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import android.content.Intent;public class ej extends Activity { private TextView stvtitle; private ListView slv; private dbUtil dbutil_1; private SimpleAdapter adapter; private List<HashMap<String, String>> list; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ejym); Intent it = getIntent(); String yh = null; final String ej1 = it.getStringExtra(yh); stvtitle=(TextView)findViewById(R.id.stvtitle); slv=(ListView)findViewById(R.id.slv); stvtitle.setText(ej1); List<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>(); list1.clear(); try{ dbutil_1=new dbUtil(); list1 = dbutil_1.ejym(ej1); System.out.println("0000000"+list1); adapter = new SimpleAdapter( this, list1, R.layout.lv02, new String[]{"biaoti"}, new int[]{R.id.stv00}); slv.setAdapter(adapter); int totalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, slv); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = slv.getLayoutParams(); params.height = totalHeight + (slv.getDividerHeight() * (slv.getCount() - 1)); slv.setLayoutParams(params); } catch(Exception e) { System.out.println("list1异常"); }}}//在mainactivity中传输正常,在ej.java中数据不显示,根据System.out.println()提示数据在httpcon.java中中断.在httpcon中初始有数据,到最后输出时数据变为空??但是同样的Imgurl数据在mainactivity.java中正常显示,而换了其他数据在ej.java显示也没问题??求教啊!!//传输数据:Imgurl /upload/comm/2010-05-26/eb6f19d7-34aa-495c-ba4a-7590297406ba.jpg/upload/comm/2010-05-27/0e127bc1-84c9-40b6-9ef2-286672fdf51f.jpg/upload/comm/2010-05-27/89927184-4c43-408a-90ad-ffdf4afaef65.jpg/upload/comm/2010-05-27/d3030916-ff43-464c-97a3-71e5398af221.jpg/upload/comm/2010-05-27/f3feee03-a12c-43f1-9eed-02a09a006521.jpg/upload/comm/2010-05-27/e32ba156-c43c-4348-b2ca-0ad0318b5878.jpg android2.2 播放视频时,截取任意时间点图像且保存下来 【在线等】 android 机器抓log android如何改变输出String值的字体大小 如何让webview响应键盘的回车事件?急! 两台手机同步显示画面技术 Android4.0上PopupWindow.update()的不解现象 android中spinner用法 如何在普通类中载入图片资源 java 写一个apk去root手机 高分求教:Android 加载so库失败 button或者imageview按下的时候变小,松开后还原 急急急。。在线等。 主题样式的问题
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class httpcon{
public ArrayList<String>GetWebServer(String methodName,ArrayList<String> Parameters,ArrayList<String> ParaValues){
ArrayList<String> Values =new ArrayList<String>();
String ServerUrl="http://10.0.2.2:58665/Service1.asmx";
String soapAction ="http://tempuri.org/" + methodName;
String soap ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body />";
String tps, vps, ts;
String mreakString = "";
mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
for(int i =0; i < Parameters.size(); i++)
{
tps = Parameters.get(i).toString();
vps = ParaValues.get(i).toString();
ts="<" + tps + ">" + vps + "</" + tps + ">";
mreakString = mreakString +ts;}
mreakString = mreakString + "</" + methodName + ">";
String soap2 = "</soap:Envelope>";
String requestData = soap + mreakString + soap2;
try{URL url = new URL(ServerUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
byte[] bytes = requestData.getBytes("utf-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(6000);// 设置超时时间
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
con.setRequestProperty("SOAPAction", soapAction);
con.setRequestProperty("Content-Length", ""+bytes.length);
OutputStream outStream = con.getOutputStream();
outStream.write(bytes);
outStream.flush();
outStream.close();
InputStream inStream = con.getInputStream();
System.out.print("流");
Values = inputStreamtovaluelist(inStream, methodName);
return Values;
} catch (Exception e) {
System.out.print("2221");
System.out.print("2221");
return null;}}
public ArrayList<String> inputStreamtovaluelist(InputStream in, String methodName) throws IOException {
StringBuffer out = new StringBuffer();
String s1 = "";
byte[] b = new byte[4096];
ArrayList<String> Values = new ArrayList<String>();
Values.clear();
for (int n; (n = in.read(b)) != -1;) {
s1 = new String(b, 0, n);
out.append(s1);
}
System.out.println(out);
String[] s13 = s1.split("><");
String ifString = methodName + "Result";
String TS = "";
String vs = "";
Boolean getValueBoolean = false;
for (int i = 0; i < s13.length; i++) {
TS = s13[i];
System.out.println(TS);
int j, k, l;
j = TS.indexOf(ifString);
k = TS.lastIndexOf(ifString);
if (j >= 0) {
System.out.println(j);
if (getValueBoolean == false) {
getValueBoolean = true;
} else {
}
if ((j >= 0) && (k > j)) {
System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
//System.out.println(TS);
l = ifString.length() + 1;
vs = TS.substring(j + l, k - 2);
//System.out.println("fff"+vs);
Values.add(vs);
System.out.println("退出" + vs);
getValueBoolean = false;
System.out.println("一");
return Values;
}
}
if (TS.lastIndexOf("/" + ifString) >= 0) {
getValueBoolean = false;
return Values;
}
if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {
k = TS.length();
//System.out.println(TS);
vs = TS.substring(7, k - 8);
//System.out.println("f"+vs);
Values.add(vs);
} }
System.out.println("二");
return Values;
}
}
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;public class dbUtil {
private ArrayList<String> arrayList = new ArrayList<String>();
private ArrayList<String> brrayList = new ArrayList<String>();
private ArrayList<String> crrayList = new ArrayList<String>();
//private ArrayList<String> drrayList = new ArrayList<String>();
private httpcon http = new httpcon(); public List<HashMap<String, String>> chakan(String Ztz) {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); arrayList.clear();
brrayList.clear();
crrayList.clear() ;
arrayList.add("Ztz");
brrayList.add(Ztz);
crrayList = http.GetWebServer("gc", arrayList, brrayList);
System.out.println("22222"+crrayList);
HashMap<String, String> tempHash = new HashMap<String, String>();
//tempHash.put("biaoti", "biaoti");
//list.add(tempHash);
if(crrayList!=null){
for(int j = 0; j < crrayList.size(); j+=1) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("biaoti", crrayList.get(j));
// hashMap.put("fabuzhe", crrayList.get(j+1));
// hashMap.put("url", crrayList.get(j+2));
list.add(hashMap);
}}
System.out.println("111111"+list);
System.out.println("chakan"+list); return list;
}
public List<HashMap<String, String>> ejym(String Ztz) {
List<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
arrayList.clear();
brrayList.clear();
crrayList.clear() ;
arrayList.add("Ztz");
brrayList.add(Ztz);
System.out.println("这是"+brrayList);
crrayList = http.GetWebServer("ejym", arrayList, brrayList);
System.out.println("crraylist"+crrayList);
if(crrayList!=null){
for(int j = 0; j < crrayList.size(); j+=1) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.clear();
hashMap.put("biaoti", crrayList.get(j));
// hashMap.put("url", crrayList.get(j+1));
list.add(hashMap);
}}
else{System.out.println("物质");}
System.out.println("list"+list);
return list;}
public List<HashMap<String, String>>url(String Title) {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
arrayList.clear();
brrayList.clear();
crrayList.clear() ;
arrayList.add("Title");
brrayList.add(Title);
System.out.println("dbutil"+brrayList);
crrayList = http.GetWebServer("url", arrayList, brrayList);
System.out.println("22url22"+crrayList);
//HashMap<String, String> tempHash = new HashMap<String, String>();
String a=null;
if(crrayList!=null){
for(int j = 0; j < crrayList.size(); j+=1) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("content", crrayList.get(j));
// hashMap.put("url", crrayList.get(j+1));
list.add(hashMap);
}}
return list;}
public String[] tpxw() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); arrayList.clear();
brrayList.clear();
crrayList.clear();
crrayList = http.GetWebServer("tpxw", arrayList, brrayList);
System.out.println("图片新闻"+crrayList);
String[] a=new String[5];
if(crrayList!=null){
for(int j = 0; j < crrayList.size(); j++) {
a[j]="http://10.0.2.2:56041/Web"+crrayList.get(j);
}}
System.out.println("a值"+a);
return a;}
}
try {
//设置Gallery的Adapter
gallery.setAdapter(new galleryadapter(this));
gallery.setSelection(0);//设置第一个图标居中
gallery.setSpacing(00);//图标之间的距离
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();}
public class galleryadapter extends BaseAdapter {
private String[] myImageURL = dbUtil_1.tpxw();
private Context mContext;
public galleryadapter(Context c){
mContext=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return myImageURL.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(this.mContext);
try{
URL aryURI = new URL(myImageURL[position]);
// 打开连接
URLConnection conn = aryURI.openConnection();
conn.connect();
// 转变为 InputStream
InputStream is = conn.getInputStream();
// 将InputStream转变为Bitmap
Bitmap bm = BitmapFactory.decodeStream(is);
// 关闭InputStream
is.close();
//添加图片
imageView.setImageBitmap(bm);
} catch (IOException e)
{
e.printStackTrace();
}
//设置布局参数
imageView.setLayoutParams(new Gallery.LayoutParams(350, 300));
return imageView;
}}
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.lang.String.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.content.Intent;public class ej extends Activity {
private TextView stvtitle;
private ListView slv;
private dbUtil dbutil_1;
private SimpleAdapter adapter;
private List<HashMap<String, String>> list;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ejym);
Intent it = getIntent();
String yh = null;
final String ej1 = it.getStringExtra(yh);
stvtitle=(TextView)findViewById(R.id.stvtitle);
slv=(ListView)findViewById(R.id.slv);
stvtitle.setText(ej1);
List<HashMap<String, String>> list1 = new ArrayList<HashMap<String, String>>();
list1.clear();
try{
dbutil_1=new dbUtil();
list1 = dbutil_1.ejym(ej1);
System.out.println("0000000"+list1);
adapter = new SimpleAdapter(
this,
list1,
R.layout.lv02,
new String[]{"biaoti"},
new int[]{R.id.stv00});
slv.setAdapter(adapter);
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, slv);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = slv.getLayoutParams();
params.height = totalHeight + (slv.getDividerHeight() * (slv.getCount() - 1));
slv.setLayoutParams(params); }
catch(Exception e)
{
System.out.println("list1异常");
}}}
//在mainactivity中传输正常,在ej.java中数据不显示,根据System.out.println()提示数据在httpcon.java中中断.在httpcon中初始有数据,到最后输出时数据变为空??但是同样的Imgurl数据在mainactivity.java中正常显示,而换了其他数据在ej.java显示也没问题??求教啊!!
//传输数据:Imgurl
/upload/comm/2010-05-26/eb6f19d7-34aa-495c-ba4a-7590297406ba.jpg
/upload/comm/2010-05-27/0e127bc1-84c9-40b6-9ef2-286672fdf51f.jpg
/upload/comm/2010-05-27/89927184-4c43-408a-90ad-ffdf4afaef65.jpg
/upload/comm/2010-05-27/d3030916-ff43-464c-97a3-71e5398af221.jpg
/upload/comm/2010-05-27/f3feee03-a12c-43f1-9eed-02a09a006521.jpg
/upload/comm/2010-05-27/e32ba156-c43c-4348-b2ca-0ad0318b5878.jpg