Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a method I wrote for handling just that. In my case I am using JSON for the data that I recieve, because it is much more compact than XML. I suggest using Google's GSON library for converting objects to and from json like that:</p> <pre><code>Gson gson = new Gson(); JsonReply result = gson.fromJson(jsonResult, JsonReply.class); </code></pre> <p>Where JsonReply is just a pojo for holding some data. You can see Google's java docs about how to use gson in your case. In addition I must say that this method works with all kinds of characters. I am using it mostly for sendign cyrillic data.</p> <pre><code>public String postAndGetResult(String script, List&lt;NameValuePair&gt; postParameters){ String returnResult = ""; BufferedReader in = null; try { HttpParams httpParameters = new BasicHttpParams(); HttpProtocolParams.setContentCharset(httpParameters, "UTF-8"); HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8"); HttpClient client = new DefaultHttpClient(httpParameters); client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); client.getParams().setParameter("http.socket.timeout", new Integer(2000)); client.getParams().setParameter("http.protocol.content-charset", "UTF-8"); httpParameters.setBooleanParameter("http.protocol.expect-continue", false); HttpPost request = new HttpPost(SERVER + script + "?sid=" + String.valueOf(Math.random())); request.getParams().setParameter("http.socket.timeout", new Integer(5000)); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( postParameters, "UTF-8"); request.setEntity(formEntity); HttpResponse response = client.execute(request); in = new BufferedReader(new InputStreamReader(response.getEntity() .getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); returnResult = sb.toString(); } catch (Exception ex) { return ""; } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } return returnResult; } </code></pre> <p>I hope this helps. Have fun :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload