记得联网权限
<uses-permission android:name="android.permission.INTERNET"/>
Strings.xml加入下面这段代码
<string name="server_name">http://193.168.4.146:8080/</string>
FastJson 解析和Gson解析需要添加jar包,没有的话可以百度去下载这两个包。
package com.example.android_parsejson;
import android.os.AsyncTask;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void parsejson(View view){
new MyTask().execute();
}
class MyTask extends AsyncTask{
@Override
protected Object doInBackground(Object... objects) {
// TODO Auto-generated method stub
try {
URL url=new URL(getString(R.string.server_name)+"person.json");
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
//设置请求方式
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
//获取结果码
int code=connection.getResponseCode();
if(code==200){
InputStream is=connection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=null;
StringBuffer stringBuffer=new StringBuffer();
while((str=br.readLine())!=null){
stringBuffer.append(str);
}
System.out.println(stringBuffer.toString());
//开始解析 json
//使用原生态
// JSONObject jsonObject=new JSONObject(stringBuffer.toString());
// int list=jsonObject.getInt("list");
// System.out.println("长度 : "+list);
// JSONArray jsonArray=jsonObject.getJSONArray("person");
//
// for (int i = 0; i < jsonArray.length(); i++) {
// JSONObject object=jsonArray.getJSONObject(i);
// int pid=object.getInt("pid");
// String pname=object.getString("name");
// int page=object.getInt("page");
// System.out.println(pid+" "+pname+" "+page);
// }
//用Gson解析
// Gson gson=new Gson();
// BigPerson bigPerson=gson.fromJson(stringBuffer.toString(), BigPerson.class);
// System.out.println(bigPerson.toString());
// List<SmallPerson> persons=bigPerson.getPersons();
// for (SmallPerson person : persons) {
// System.out.println(person.toString());
// }
//FastJson 解析
BigPerson bigPerson=JSON.parseObject(stringBuffer.toString(),BigPerson.class);
System.out.println(bigPerson.getList()+"");
List<SmallPerson>persons=bigPerson.getPersons();
for (SmallPerson person : persons) {
System.out.println(person.toString());
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
return null;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
}