Android 使用url解析json
我正在尝试使用url解析json。我可以通过使用临时url来实现它,但是当我使用主主机url时,json会返回html值吗 临时URLAndroid 使用url解析json,android,json,parsing,url,Android,Json,Parsing,Url,我正在尝试使用url解析json。我可以通过使用临时url来实现它,但是当我使用主主机url时,json会返回html值吗 临时URL (http://md-plesk-web9.webhostbox.net:8880/sitepreview/http/glossymob.asia/Webservice.asmx/SpecificCategory?category=real%20estate&location=Madurai) 我的代码 // url to make request p
(http://md-plesk-web9.webhostbox.net:8880/sitepreview/http/glossymob.asia/Webservice.asmx/SpecificCategory?category=real%20estate&location=Madurai)
我的代码
// url to make request
private static String url = "http://md-plesk-web9.webhostbox.net:8880/sitepreview/http/glossymob.asia/Webservice.asmx/SpecificCategory?category=hotel&location=Madurai";
// JSON Node names
private static final String TAG_MAIN ="hotel";
private static final String TAG_VALUE = "Madurai";
private static final String TAG_ID = "id";
private static final String TAG_CATEGORY = "category";
private static final String TAG_TYPE = "type";
private static final String TAG_NAME = "name";
private static final String TAG_BANNER = "banner";
private static final String TAG_LOGO = "logo";
private static final String TAG_ABOUT = "about";
private static final String TAG_CONTACT = "contact";
private static final String TAG_MAP = "map";
private static final String TAG_MAP_LAT_LONG = "lat_long_1";
private static final String TAG_MAPLATITUDE = "latitude";
private static final String TAG_MAPLONGITUDE = "longitude";
private static final String TAG_CATEGORY_TOTAL = "total";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
Log.v("exact string value", json.toString());
try {
// Getting Array of Contacts
JSONObject js= json.getJSONObject(TAG_MAIN);
int ival = js.getInt(TAG_CATEGORY_TOTAL);
contacts = js.getJSONArray(TAG_VALUE);
Log.v("Total:", ""+ival);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String category = c.getString(TAG_CATEGORY);
String type = c.getString(TAG_TYPE);
String name = c.getString(TAG_NAME);
String banner = c.getString(TAG_BANNER);
String logo = c.getString(TAG_LOGO);
String about = c.getString(TAG_ABOUT);
String contact = c.getString(TAG_CONTACT);
Log.v("Values:", id+"--"+category+"--"+type+"--"+name+"--"+banner+"--"+logo+"--"+about+"--"+contact);
// Phone number is agin JSON Object
JSONObject mapval = c.getJSONObject(TAG_MAP);
JSONArray con = js.getJSONArray(TAG_VALUE);
JSONObject mapvalues = con.getJSONObject(0);
String latitude = mapvalues.getString(TAG_MAPLATITUDE);
String longitude = mapvalues.getString(TAG_MAPLONGITUDE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_CATEGORY, category);
map.put(TAG_TYPE, type);
map.put(TAG_NAME, name);
/*Log.v("Received json values", ""+map.toString());*/
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
当我试图替换这个时
主URL
我得到的价值观是
01-02 11:07:27.116:V/json字符串值(29330):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
运行时错误
正文{字体系列:“Verdana”;字体重量:正常;字体大小:.7em;颜色:黑色;}p{字体系列:“Verdana”;字体重量:正常;颜色:黑色;页边空白顶部:-5px}b{字体系列:“Verdana”;字体重量:粗体;颜色:黑色;页边空白顶部:-5px}H1{字体系列:“Verdana”;字体重量:正常;字体大小:18pt;颜色:红色}H2字体系列:“Verdana”;字体重量:正常;字体大小:14pt;颜色:栗色}字体系列:“Lucida控制台”;字体大小:.9em}。标记{字体重量:粗体;颜色:黑色;文本装饰:无;}。版本{颜色:灰色;}。错误{页边距底部:10px;}。可扩展{文本装饰:下划线;字体大小:粗体;颜色:海军蓝;光标:手;}
“/”应用程序中出现服务器错误。
运行时错误
描述:服务器上发生应用程序错误。此应用程序的当前自定义错误设置阻止远程查看应用程序错误的详细信息(出于安全原因)。但是,可以通过在本地服务器计算机上运行的浏览器查看应用程序错误。
详细信息:要在远程计算机上查看此特定错误消息的详细信息,请在当前web应用程序根目录中的“web.config”配置文件中创建customErrors标记。然后,此customErrors标记的“mode”属性应设置为“Off”。
!--Web.Config配置文件--Configuration system.Web customErrors mode=“Off”//system.Web/Configuration
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
注意:通过修改应用程序的customErrors配置标记的“defaultRedirect”属性以指向自定义错误页URL,可以将当前看到的错误页替换为自定义错误页。
!--Web.Config配置文件--Configuration system.Web customErrors mode=“RemoteOnly”defaultRedirect=“mycustompage.htm”//system.Web/Configuration
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
我通过替换字符串中的Xml数据来处理Xml数据,我的json返回确切的json字符串值。
包com.androidhive.jsonparsintest
导入java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.util.Log;
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
公共JSONObject getJSONFromUrl(字符串url){
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
行=行。替换(“,”);
行=行。替换(“,”);
行=行。替换(“,”);
Log.v(“字符串值”,行);
某人附加(行);
//Log.v(“sbstring值”,sb.toString());
}
is.close();
json=sb.toString();
Log.v(“json字符串值”,json);
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
}
您的回答是:-
XMLParser parser = new XMLParser();
JSONArray jsonRes = parser.parse(response.getEntity().getContent());
// response is the HTTPResponse object
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
{“房地产”:{“Madurai”:[{“id”:“5”,“category”:“real estate”,“type”:“Silver”,“name”:“Heera”,“banner”:“www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg”,“logo”:“www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg”,“about”:“test”,“test”,“map”:{“lation”:“lation”:“lation”:“10.00”,“long”:“9.50”},“total}
现在,首先需要替换为
public class XMLParser extends DefaultHandler {
private static String ROOT_ELEMENT = "string";
private StringBuffer data = new StringBuffer();
private JSONArray jsonData;
public XMLParser() {
}
public JSONArray parse(InputStream in) {
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(in, this);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return jsonData;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
data.replace(0, data.length(), "");
if (localName.equalsIgnoreCase(ROOT_ELEMENT)) {
jsonData = new JSONArray();
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
data.append(ch, start, length);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase(ROOT_ELEMENT)){
try {
jsonData = new JSONArray(data.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
responseString=responseString.replace(“,”);
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
responseString=responseString.replace(“,”);
请在替换上述行后将字符串转换为JSONObject。它不是纯JSON。它包含XML标记。可以仅使用XML,也可以使用JSON
如果您只需删除顶部和底部的XML标记,并继续进行JSON解析,这将很容易。您将在
内部获得JSON响应,即XML,因此首先必须从该标记检索JSON数据
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
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);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>","");
line = line.replace("<string xmlns=\"http://tempuri.org/\">","");
line = line.replace("</string>","");
Log.v("string value", line);
sb.append(line);
//Log.v("sbstring value", sb.toString());
}
is.close();
json = sb.toString();
Log.v("json string value", json);
} 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;
}
}
<string xmlns="http://tempuri.org/">
{"real estate":{"Madurai":[{"id":"5","category":"real estate","type":"Silver","name":"Heera","banner":"www.glossymob.com/soli/MapMadurai/5_banner_laptop-computer.jpg","logo":"www.glossymob.com/soli/MapMadurai/5_logo_laptop-computer.jpg","about":"test","contact":"test","map":{"lat_long_1":{"latitude":"10.00","longitude":"9.50"}}} ],"total":1} }
</string>
{“真实的”
“地产”