首页 新闻 会员 周边 捐助

很诡异的android与webservice数据交互问题

0
悬赏园豆:100 [已解决问题] 解决于 2013-03-26 14:47

困扰好几周了,希望有时间的帮忙看看,谢了

传输数据(title,imgurl,account)当传输title或title+account时数据正常传输,但是无法传输Imgurl项,即使imgurl+title都一样,没有任何数据显示
  请教

1.httpcon.java
2.dbUtil.java
复制内容到剪贴板

代码:

View Code
View Code
  1 //数据交互,输入输出流与读取流
  2 import java.io.IOException;
  3 import java.io.InputStream;
  4 import java.io.OutputStream;
  5 import java.net.HttpURLConnection;
  6 import java.net.URL;
  7 import java.util.ArrayList;
  8 public class httpcon{
  9 public ArrayList<String>GetWebServer(String methodName,ArrayList<String> Parameters,ArrayList<String> ParaValues){
 10 ArrayList<String> Values =new ArrayList<String>();
 11 //soap格式
 12 String ServerUrl="http://10.0.2.2:58665/Service1.asmx";
 13 String soapAction ="http://tempuri.org/" + methodName;
 14 String soap ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 15 + "<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/\">"
 16 + "<soap:Body />";
 17 String tps, vps, ts;
 18 String mreakString = "";
 19 mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
 20 for(int i =0; i < Parameters.size(); i++)
 21 {
 22 tps = Parameters.get(i).toString();
 23 vps = ParaValues.get(i).toString();
 24 ts="<" + tps + ">" + vps + "</" + tps + ">";
 25 mreakString = mreakString +ts;}
 26 mreakString = mreakString + "</" + methodName + ">";
 27 String soap2 = "</soap:Envelope>";
 28 String requestData = soap + mreakString + soap2;
 29 //连接webservice,数据交互
 30 try{URL url = new URL(ServerUrl);
 31 HttpURLConnection con = (HttpURLConnection) url.openConnection();
 32 byte[] bytes = requestData.getBytes("utf-8");
 33 con.setDoInput(true);
 34 con.setDoOutput(true);
 35 con.setUseCaches(false);
 36 con.setConnectTimeout(6000);// 设置超时时间
 37 con.setRequestMethod("POST");
 38 con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
 39 con.setRequestProperty("SOAPAction", soapAction);
 40 con.setRequestProperty("Content-Length", ""+bytes.length);
 41 OutputStream outStream = con.getOutputStream();
 42 outStream.write(bytes);
 43 outStream.flush();
 44 outStream.close();
 45 InputStream inStream = con.getInputStream();
 46 //读取流类调用
 47 Values = inputStreamtovaluelist(inStream, methodName);
 48 return Values;
 49 } catch (Exception e) {
 50 System.out.print("2221");
 51 System.out.print("2221");
 52 return null;}}
 53 //读取流
 54 public ArrayList<String> inputStreamtovaluelist(InputStream in, String methodName) throws IOException {
 55 StringBuffer out = new StringBuffer();
 56 String s1 = "";
 57 byte[] b = new byte[4096];
 58 ArrayList<String> Values = new ArrayList<String>();
 59 Values.clear();
 60 for (int n; (n = in.read(b)) != -1;) {
 61 s1 = new String(b, 0, n);
 62 out.append(s1);
 63 }
 64 System.out.println(out);
 65 String[] s13 = s1.split("><");
 66 String ifString = methodName + "Result";
 67 String TS = "";
 68 String vs = "";
 69 Boolean getValueBoolean = false;
 70 for (int i = 0; i < s13.length; i++) {
 71 TS = s13;
 72 System.out.println(TS);
 73 int j, k, l;
 74 j = TS.indexOf(ifString);
 75 k = TS.lastIndexOf(ifString);
 76 if (j >= 0) {
 77 System.out.println(j);
 78 if (getValueBoolean == false) {
 79 getValueBoolean = true;
 80 } else {
 81 }
 82 if ((j >= 0) && (k > j)) {
 83 System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
 84 //System.out.println(TS);
 85 l = ifString.length() + 1;
 86 vs = TS.substring(j + l, k - 2);
 87 //System.out.println("fff"+vs);
 88 Values.add(vs);
 89 System.out.println("退出" + vs);
 90 getValueBoolean = false;
 91 System.out.println("");
 92 return Values;
 93 }
 94 }
 95 if (TS.lastIndexOf("/" + ifString) >= 0) {
 96 getValueBoolean = false;
 97 return Values;
 98 }
 99 if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {
100 k = TS.length();
101 //System.out.println(TS);
102 vs = TS.substring(7, k - 8);
103 //System.out.println("f"+vs);
104 Values.add(vs);
105 } }
106 System.out.println("");
107 return Values;
108 }}
109 数据交互,输入输出流与读取流
110 import java.io.IOException;
111 import java.io.InputStream;
112 import java.io.OutputStream;
113 import java.net.HttpURLConnection;
114 import java.net.URL;
115 import java.util.ArrayList;
116 public class httpcon{
117 public ArrayList<String>GetWebServer(String methodName,ArrayList<String> Parameters,ArrayList<String> ParaValues){
118 ArrayList<String> Values =new ArrayList<String>();
119 //soap格式
120 String ServerUrl="http://10.0.2.2:58665/Service1.asmx";
121 String soapAction ="http://tempuri.org/" + methodName;
122 String soap ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"
123 + "<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/\">"
124 + "<soap:Body />";
125 String tps, vps, ts;
126 String mreakString = "";
127 mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
128 for(int i =0; i < Parameters.size(); i++)
129 {
130 tps = Parameters.get(i).toString();
131 vps = ParaValues.get(i).toString();
132 ts="<" + tps + ">" + vps + "</" + tps + ">";
133 mreakString = mreakString +ts;}
134 mreakString = mreakString + "</" + methodName + ">";
135 String soap2 = "</soap:Envelope>";
136 String requestData = soap + mreakString + soap2;
137 //连接webservice,数据交互
138 try{URL url = new URL(ServerUrl);
139 HttpURLConnection con = (HttpURLConnection) url.openConnection();
140 byte[] bytes = requestData.getBytes("utf-8");
141 con.setDoInput(true);
142 con.setDoOutput(true);
143 con.setUseCaches(false);
144 con.setConnectTimeout(6000);// 设置超时时间
145 con.setRequestMethod("POST");
146 con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
147 con.setRequestProperty("SOAPAction", soapAction);
148 con.setRequestProperty("Content-Length", ""+bytes.length);
149 OutputStream outStream = con.getOutputStream();
150 outStream.write(bytes);
151 outStream.flush();
152 outStream.close();
153 InputStream inStream = con.getInputStream();
154 //读取流类调用
155 Values = inputStreamtovaluelist(inStream, methodName);
156 return Values;
157 } catch (Exception e) {
158 System.out.print("2221");
159 System.out.print("2221");
160 return null;}}
161 //读取流
162 public ArrayList<String> inputStreamtovaluelist(InputStream in, String methodName) throws IOException {
163 StringBuffer out = new StringBuffer();
164 String s1 = "";
165 byte[] b = new byte[4096];
166 ArrayList<String> Values = new ArrayList<String>();
167 Values.clear();
168 for (int n; (n = in.read(b)) != -1;) {
169 s1 = new String(b, 0, n);
170 out.append(s1);
171 }
172 System.out.println(out);
173 String[] s13 = s1.split("><");
174 String ifString = methodName + "Result";
175 String TS = "";
176 String vs = "";
177 Boolean getValueBoolean = false;
178 for (int i = 0; i < s13.length; i++) {
179 TS = s13;
180 System.out.println(TS);
181 int j, k, l;
182 j = TS.indexOf(ifString);
183 k = TS.lastIndexOf(ifString);
184 if (j >= 0) {
185 System.out.println(j);
186 if (getValueBoolean == false) {
187 getValueBoolean = true;
188 } else {
189 }
190 if ((j >= 0) && (k > j)) {
191 System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
192 //System.out.println(TS);
193 l = ifString.length() + 1;
194 vs = TS.substring(j + l, k - 2);
195 //System.out.println("fff"+vs);
196 Values.add(vs);
197 System.out.println("退出" + vs);
198 getValueBoolean = false;
199 System.out.println("");
200 return Values;
201 }
202 }
203 if (TS.lastIndexOf("/" + ifString) >= 0) {
204 getValueBoolean = false;
205 return Values;
206 }
207 if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {
208 k = TS.length();
209 //System.out.println(TS);
210 vs = TS.substring(7, k - 8);
211 //System.out.println("f"+vs);
212 Values.add(vs);
213 } }
214 System.out.println("");
215 return Values;
216 }}

 

2.dbutil.java
View Code
//算是逻辑层吧
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主要代码
View Code
//显示网络图片,从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中初始有数据,到最后输出时数据变为空??测试数据中断在 inputStreamtovaluelist中,但是同样的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


 
日清日高的主页 日清日高 | 初学一级 | 园豆:4
提问于:2013-03-20 11:45
< >
分享
最佳答案
1

这两个方法都不一样 你怎么比较的  

private String[] myImageURL = dbUtil_1.tpxw();//imgurl传输正常
list1=dbutil_1.ejym(ej1);

首先 方法名不一样 参数也不同 实现也不一样 你这个结果不一样 不很正常
如果是 第二个有问题 那就测试 第二个 不要拿两个比较说事
代码 看着真累
第二个 有参数的 不知结果 是否 本来为空 ???
            
收获园豆:100
tianxiaozz | 菜鸟二级 |园豆:322 | 2013-03-21 09:46

首先感谢你帮忙。代码是长了点,我也很无奈啊,不是后边的问题,测试数据流实在httpcon.java中中断的。可能是符号在inputstreamtovaluelist中转化的问题吧

日清日高 | 园豆:4 (初学一级) | 2013-03-21 09:55

@日清日高: 那你就直接测试第二个方法就好   写测试的例子 不在主程序里找错 避免主程序有错 影响 查错

tianxiaozz | 园豆:322 (菜鸟二级) | 2013-03-21 12:35

@tianxiaozz: 

貌似问题在httpcon.java 64行正常时输出上图  ,异常时下图 。最后一行不知为啥多了个<string/>然后就会解析出问题

日清日高 | 园豆:4 (初学一级) | 2013-03-22 09:16

@日清日高: 问题找到了,就是多了个空参,导致无法正确分割字符串

日清日高 | 园豆:4 (初学一级) | 2013-03-26 14:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册