首先使用java代码实现网络的连接。
定义 HttpUtil类。
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtil {
public static String getJsonContent(String url_path) {
InputStream inputStream = null;
try {
URL url = new URL(url_path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(3000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
int code = connection.getResponseCode();
System.out.println("code=================" + code);
if (code == 200) {
inputStream = connection.getInputStream();
return chageInputStream(inputStream);// 调用下面的方法,将流变为字符返回。
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
private static String chageInputStream(InputStream inputStream) {
// TODO Auto-generated method stub
String jsonString = "";
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
int len = 0;
byte[] data = new byte[1024];
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
bStream.write(data, 0, len);
}
jsonString = new String(bStream.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonString;
}
}
Person类我就不给大家列举了。
接下来定义了一个JsonTool类,实现JSON数据的解析。
import org.json.JSONObject;
public class JsonTool {
public static Person getPerson(String key, String jsonString) {
Person person = new Person();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject personObject=jsonObject.getJSONObject("person");//获取到key值。
person.setId(personObject.getInt("id"));
person.setName(personObject.getString("name"));
person.setAddress(personObject.getString("address"));
} catch (Exception e) {
// TODO: handle exception
}
return person;
}
}
当然我们在主界面就是添加了一个Button,点击触发事件,进行解析。
public class MainActivity extends Activity {
Button button;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.person);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String patString = "http://192.168.3.2:8080/TestServer/servlet/JsonAction?action_flag=person";
String jsonString = HttpUtil.getJsonContent(patString);
Person person = JsonTool.getPerson("person", jsonString);
System.out.println(person.getName());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
因为访问了网络,需要添加一个网络权限。anroid.permission.INTERNET
服务器端的代码大家可以参考这篇博客http://blog.csdn.net/shyboyes/article/details/38419807。
同时,工程文件在http://download.csdn.net/detail/fengzy1990/7730683可以下载。