Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to post JSON String from Android
    primarykey
    data
    text
    <p>I am facing problem when try to post json on server from Android . The error is: </p> <blockquote> <p>Failed loading JSON. Special characters must not be included in the request. Please check the requested JSON.</p> </blockquote> <p>I have followed many examples, but none were helpful. Please suggest a solution or help me find the problem in the code.</p> <p>Below is the code for posting JSON string on server.</p> <pre><code>public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); /* ====================================================================================================*/ JSONObject listobj = new JSONObject (); JSONObject listInvoice = new JSONObject (); listInvoice.put("client_id",""); listInvoice.put("date_from",""); listInvoice.put("date_to",""); listInvoice.put("invoice_number",""); listInvoice.put("invoice_record_status",""); listInvoice.put("invoice_status",""); listInvoice.put("page","1"); listInvoice.put("per_page_record","10"); listobj.put("listInvoice", listInvoice); //--List nameValuePairs = new ArrayList(1); List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(1); nameValuePairs.add(new BasicNameValuePair("json_data", listobj.toString())); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); Log.d("JSON",listobj.toString()); /*======================================================================================================*/ HttpResponse httpResponse = httpClient.execute(httpPost); String is = EntityUtils.toString(httpResponse.getEntity()); Log.d("JSON","RESPONSE : " + is); //--HttpEntity httpEntity = httpResponse.getEntity(); //--is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { Log.e("JSON",e.getMessage()); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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