<pre name="code" class="html">xml格式:
</pre><pre name="code" class="html">{
"fangone":
[
"shuoming": "大飘窗搭配绿色乡村风格,春意盎然",
"quyu":"长宁区",
"louceng": "5/13层",
"mianji": "14.0m*m",
"chaoxiang": "朝向",
"ditie": "地铁沿线",
"WC": "卫生间",
"dianshi": "有电视和阳台",
"baojia": "2310元/月",
]
}
</pre><pre name="code" class="html">
<pre name="code" class="html">/**
* 获取json数据,并解析
* @author wu
*
*/
public class MainActivity extends Activity {
List<Information> Infolist;
String strUrl="这里是链接"; //网页链接
String getResult; //从网页返回的Json数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Infolist=new ArrayList<Information>();
getResult = connServerForResult(strUrl);
Infolist=parseJson(getResult);
for (int i = 0; i < Infolist.size(); i++) {
Log.i("Prt", Infolist.get(i).getShuoming()+Infolist.get(i).getQuyu()); //等等……不一一输出了
}
}
/**
* 通过URL从服务器上下载下来,保存为字符串
* @param strUrl
* @return
*/
public static String connServerForResult(String strUrl) {
// HttpGet对象
HttpGet httpRequest = new HttpGet(strUrl);
String strResult = "";
try {
// HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);
// 当返回码为200时,做处理 得到服务器端返回json数据,并做处理
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
/**
* 普通Json数据解析
* @param strResult 从URL中得到的字符串
*/
public static List<Information> parseJson(String strResult) {
Information mInformation = new Information();
List<Information> mList = new ArrayList<Information>();
try {
JSONObject mjsonObj = new JSONObject(strResult);
JSONArray mJsonArray=mjsonObj.getJSONArray("fangone");
mInformation.setShuoming(mJsonArray.getString(0));
mInformation.setQuyu(mJsonArray.getString(1));
mInformation.setLouceng(mJsonArray.getString(2));
mInformation.setMianji(mJsonArray.getString(3));
mInformation.setChaoxiang(mJsonArray.getString(4));
mInformation.setDitie(mJsonArray.getString(5));
mInformation.setWC(mJsonArray.getString(6));
mInformation.setDianshi(mJsonArray.getString(7));
mInformation.setBaojia(mJsonArray.getString(8));
mList.add(mInformation);
} catch (JSONException e) {
System.out.println("Json parse error");
e.printStackTrace();
}
return mList;
}
}
<pre name="code" class="html">xml格式:
</pre><pre name="code" class="html">{
"fangone":
[
"shuoming": "大飘窗搭配绿色乡村风格,春意盎然",
"quyu":"长宁区",
"louceng": "5/13层",
"mianji": "14.0m*m",
"chaoxiang": "朝向",
"ditie": "地铁沿线",
"WC": "卫生间",
"dianshi": "有电视和阳台",
"baojia": "2310元/月",
]
}
</pre><pre name="code" class="html">
<pre name="code" class="html">/**
* 获取json数据,并解析
* @author wu
*
*/
public class MainActivity extends Activity {
List<Information> Infolist;
String strUrl="这里是链接"; //网页链接
String getResult; //从网页返回的Json数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Infolist=new ArrayList<Information>();
getResult = connServerForResult(strUrl);
Infolist=parseJson(getResult);
for (int i = 0; i < Infolist.size(); i++) {
Log.i("Prt", Infolist.get(i).getShuoming()+Infolist.get(i).getQuyu()); //等等……不一一输出了
}
}
/**
* 通过URL从服务器上下载下来,保存为字符串
* @param strUrl
* @return
*/
public static String connServerForResult(String strUrl) {
// HttpGet对象
HttpGet httpRequest = new HttpGet(strUrl);
String strResult = "";
try {
// HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获得HttpResponse对象
HttpResponse httpResponse = httpClient.execute(httpRequest);
// 当返回码为200时,做处理 得到服务器端返回json数据,并做处理
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的数据
strResult = EntityUtils.toString(httpResponse.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return strResult;
}
/**
* 普通Json数据解析
* @param strResult 从URL中得到的字符串
*/
public static List<Information> parseJson(String strResult) {
Information mInformation = new Information();
List<Information> mList = new ArrayList<Information>();
try {
JSONObject mjsonObj = new JSONObject(strResult);
JSONArray mJsonArray=mjsonObj.getJSONArray("fangone");
mInformation.setShuoming(mJsonArray.getString(0));
mInformation.setQuyu(mJsonArray.getString(1));
mInformation.setLouceng(mJsonArray.getString(2));
mInformation.setMianji(mJsonArray.getString(3));
mInformation.setChaoxiang(mJsonArray.getString(4));
mInformation.setDitie(mJsonArray.getString(5));
mInformation.setWC(mJsonArray.getString(6));
mInformation.setDianshi(mJsonArray.getString(7));
mInformation.setBaojia(mJsonArray.getString(8));
mList.add(mInformation);
} catch (JSONException e) {
System.out.println("Json parse error");
e.printStackTrace();
}
return mList;
}
}