2012-11-09 49 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