2012-11-09 50 views
0

逃離報價,我試圖讓一個Web服務調用,我必須要通過在Java

login.php?message=[{"email":"[email protected]","password":"tiger"}] 

我已經用反斜槓轉義雙引號這樣

String weblink = "login.php?message=[{\"email\":\"[email protected]\",\"password\":\"tiger\"}]"; 

但我米仍然有錯誤。我試着打電話給其他沒有任何雙引號需要數據的Web服務,他們工作正常,所以我很確定問題出在這裏。此外,我得到一個異常的java.lang說

java.lang.Exception Indicates a serious configuration error.DateParseException An exception to indicate an error parsing a date string. DestroyFailedException Signals that the destroy() method failed   

編輯: 我使用的URLEncoder和JSON對象,但仍得到一個錯誤

這裏試過是代碼

String HOST = "http://62.285.107.329/disaster/webservices/"; 

String weblink = "login.php?message=[{\"email\":\"[email protected]\",\"password\":\"tiger\"}]"; 
String result = callWebservice(weblink); 

public String callWebservice(String weblink) { 
    String result = ""; 
    try { 

     HttpParams httpParameters = new BasicHttpParams(); 
     int timeoutConnection = 7500; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 
       timeoutConnection); 
     int timeoutSocket = 7500; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
     HttpClient client = new DefaultHttpClient(httpParameters); 
     HttpGet request = new HttpGet(); 
     URI link = new URI(HOST + weblink); 
     request.setURI(link); 
     HttpResponse response = client.execute(request); 

     BufferedReader rd = new BufferedReader(new InputStreamReader(
       response.getEntity().getContent())); 
     result = rd.readLine(); 

    } catch (Exception e1) { 
     e1.printStackTrace(); 
     result = "timeout"; 
    } 
    return result; 
} 
其餘

此外web服務返回一個JSON對象,所以這也可能是錯誤的原因?

+0

異常變得沒有意義。發佈堆棧跟蹤。 – Doorknob

+0

如果你提供了錯誤,它可能會幫助更多。 – scrappedcola

回答

0

你必須使用%22代替"爲:login.php?message=[{%22email%22:%[email protected]%22,%22password%22:%22tiger%22}]

"不是一個URL的有效字符。

一個更普遍的解決方案是使用URLEncoder.encode("login.php?message=[{\"email\":\"[email protected]\",\"password\":\"tiger\"}]", "UTF8")

+0

使用%22我得到一個URI錯誤,說無效字符 – Amanni

+0

URLEncoder函數比「」編碼更多的字符。你確定它說那個字符無效嗎? – thedayofcondor

4

,而不是用手嘗試這一點,並收到錯誤,爲什麼不使用使用JSONObject類和類URLEncoder的組合。

JSONObject json = new JSONObject(); 
json.put("email","[email protected]"); 
json.put("password", "tiger"); 
String s = "login.php?message=" + UrlEncoder.encode(json.toString()); 
0

當您與網絡服務通信時,您需要URL encode您的數據。在你的情況下,URL編碼將替換"%22,但如果你要添加其他特殊字符,如%,編碼也會捕獲這些。 JDK中有一個java類,叫做URLEncoder

所以,基本上,你會做的是使用URLEncoder.encode()準備你的字符串,像這樣:

String weblink = URLEncoder.encode("login.php?message=[{\"email\":\"[email protected]\",\"password\":\"tiger\"}]"); 

現在該字符串編碼,你應該能夠一起發送到服務器,並有它明白你的意思。

0

向所有人道歉,但似乎問題是我試圖以錯誤的方式使用web服務,因爲它返回一個JSON對象。

的正確方法的人誰可能會遇到這樣做,這是在下面的代碼

String str="url"; 
try{ 
    URL url=new URL(str); 
    URLConnection urlc=url.openConnection(); 
    BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream())); 
    String line; 
    while((line=bfr.readLine())!=null) 
    { 
    JSONArray jsa=new JSONArray(line); 
    for(int i=0;i<jsa.length();i++) 
     { 
     JSONObject jo=(JSONObject)jsa.get(i); 
        title=jo.getString("deal_title"); //tag name "deal_title",will return value that we save in title string 
       des=jo.getString("deal_description"); 
    } 
} 
catch(Exeption e){ 
} 

這個答案是從

How to call a json webservice through android