困擾幾周了,請教啊,android與websevice資料互動很詭異的問題

來源:互聯網
上載者:User

標籤:des   android   blog   http   io   os   ar   java   for   

============問題描述============


傳輸資料(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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.