首页 > 代码库 > 困扰几周了,请教啊,android与websevice数据交互很诡异的问题
困扰几周了,请教啊,android与websevice数据交互很诡异的问题
============问题描述============
传输数据(title,imgurl,account)当传输title或title+account时数据正常传输,但是无法传输Imgurl项,即使imgurl+title都一样,没有任何数据显示
请教
1.httpcon.java复制内容到剪贴板代码:
//数据交互,输入输出流与读取流
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>();
//soap格式
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;
//连接webservice,数据交互
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();
//读取流类调用
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;
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.java复制内容到剪贴板代码:
//算是逻辑层吧
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();
/*只粘贴有用的两个调用*/
//ej.java调用
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);// soap格式
方法名,参数名,参数值
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;}
//mainactivity.java调用,传参为imgurl
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);//soap格式
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);//补足路径,imgurl存储的为相对路径
return a;}
}
3.mainactivity.java主要代码
复制内容到剪贴板代码:
//显示网络图片,从dbutil.ava获取imgurl路径
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();//imgurl传输正常
//gallery格式
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复制内容到剪贴板代码:
//调用dbutil.ava,用listview显示所得Imgurl数据
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传参,在此未粘出代码,但是传参肯定没问题
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(//adapter填充数据,但是到这儿数据已经为空
list,
R.layout.lv02,
new String[]{"biaoti"},
new int[]{R.id.stv00});
slv.setAdapter(adapter);
int totalHeight = 0;
//获取listview项目总数
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
============解决方案1============
不敢当,只不过是我这几天刚好也遇到了这个问题,你按照我3楼和7楼所写的修改,程序就能跑起来了。我的已经能正确运行了。
困扰几周了,请教啊,android与websevice数据交互很诡异的问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。