如何从Android发布JSON字符串

编程入门 行业动态 更新时间:2024-10-27 10:33:28
本文介绍了如何从Android发布JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当尝试从Android在服务器上发布json时遇到问题.错误是:

I am facing problem when try to post json on server from Android . The error is:

无法加载JSON.特殊字符不得包含在 要求.请检查请求的JSON.

Failed loading JSON. Special characters must not be included in the request. Please check the requested JSON.

我遵循了许多示例,但没有一个有用的例子.请提出解决方案或帮助我在代码中找到问题.

I have followed many examples, but none were helpful. Please suggest a solution or help me find the problem in the code.

下面是在服务器上发布JSON字符串的代码.

Below is the code for posting JSON string on server.

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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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; } } }

推荐答案

这是将json传递到您的URL的功能

this is the function for passing json to your URL

public void getServerData() throws JSONException, ClientProtocolException, IOException { String title = Title_edittext.getText().toString().trim(); String details = Details_edittext.getText().toString().trim(); HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC); HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); HttpClient client = new DefaultHttpClient(httpParams); HttpPost request = new HttpPost(url); // add your url here... request.setHeader( "Content-Type", "application/json" ); JSONObject json = new JSONObject(); json.put("client_id", channel_token); json.put("date_from", data_src_id); json.put("date_to", title); Log.i("JSON Object", json.toString()); StringEntity se = new StringEntity(json.toString()); se.setContentEncoding("UTF-8"); se.setContentType("application/json"); request.setEntity(se); HttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); InputStream is = entity.getContent(); String _response = convertStreamToString(is); System.out.println("res-- " + _response); // Check if server response is valid code int res_code = response.getStatusLine().getStatusCode(); System.out.println("code-- " +res_code); } private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append((line + "\n")); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }

更多推荐

如何从Android发布JSON字符串

本文发布于:2023-10-12 00:00:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Android   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!