如何在android中对发送消息进行Json解析
以下是Json函数代码:如何在android中对发送消息进行Json解析,android,Android,以下是Json函数代码: new sendMesgTask().execute(Sendername + ": Request For Travlling to You"); 当尝试运行此代码时,我得到的是异常,而不是RESTE代码 下面我得到的例外是错误: 日志目录: >09-11:32:48.805:E/价值观(15432): 请求&通知类型=1&通知ID=50011&通知状态=false 09-11 11:32:48.805:I/Sys
new sendMesgTask().execute(Sendername
+ ": Request For Travlling to You");
当尝试运行此代码时,我得到的是异常,而不是RESTE代码
下面我得到的例外是错误:
日志目录:
url参数中有一个空格字符。“旅行请求”中的空格。请使用URLEncoder.encode()before.错误告诉了所有人。您的查询参数中有一个非法字符。At 87.Request&Notificationtype=1¬ificationId=50011¬ify\u status=false当我输入BroserYes时,请查看此工作正常。因为浏览器知道如何发送空格。它将在发送之前对字符串进行url编码。所有空格都将替换为%20。问题仍然相同我已经添加了test=JSONfunctions.getJSONfromURL(Uri.encode(sendmsgurl));JSONObject\u jobj=new JSONObject(test);请告诉如何编码或如何替换间距请建议只使用您的url.replaceAll(“,“%20”);test=JSONfunctions.getJSONfromURL(urlcoder.encode(sendmsgurl));这个错误来自方法encode(字符串)从类型来看,URLEncoder已弃用
new sendMesgTask().execute(Sendername
+ ": Request For Travlling to You");
public static String getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
httppost.setHeader("Accept", "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,/;q=0.8");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return result;
}