首先,我们需要建立服务端,这里就用java的一个servlet作为示例吧
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
JSONObject person = new JSONObject();
person.put("name", "小明");
JSONArray phone = new JSONArray();
phone.add("1333444");
phone.add("021333");
person.put("phone", phone);
JSONObject address = new JSONObject();
address.put("provice", "安徽");
address.put("city", "合肥");
person.put("address", address);
out.print(person.toString());
}
直接通过浏览器访问,我们可以得到如下结果
{"name":"小明","phone":["1333444","021333"],"address":{"provice":"安徽","city":"合肥"}}
关于JSON的结果,这里就不详细介绍了,可以看出这里phone有两个,地址又另外分出两个属性,基本将所有可能都包含了。android端我们选用XUtils框架来写,主要由于android的html请求原生的相当复杂,涉及到ui线程及多线程,这些就让框架给我们做吧。
这里贴出布局的代码跟样式
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hello.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="28dp"
android:text="姓名" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:text="电话" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/textView4"
android:layout_marginLeft="13dp"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/name"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/provice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/textView4"
android:ems="10"
android:inputType="textPostalAddress" />
<EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/provice"
android:layout_alignTop="@+id/textView5"
android:ems="10"
android:inputType="textPostalAddress" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/phone1"
android:layout_marginTop="48dp"
android:text="地址" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_marginTop="24dp"
android:layout_toRightOf="@+id/textView3"
android:text="省份" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/textView3"
android:text="城市" />
<EditText
android:id="@+id/phone2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/phone1"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="phone" />
</RelativeLayout>
首先,声明好各个View,利用 Xutils注入他们的值
@ViewInject(R.id.button1)
private Button button;
@ViewInject(R.id.name)
private EditText name;
@ViewInject(R.id.phone1)
private EditText phone1;
@ViewInject(R.id.phone2)
private EditText phone2;
@ViewInject(R.id.provice)
private EditText provice;
@ViewInject(R.id.city)
private EditText city;
记得在oncreate方法里面加入
ViewUtils.
inject
(
this
);这句代码
private JSONArray getJOS(String jsonText) {
JSONTokener jsparser = new JSONTokener(jsonText);
JSONArray jos = new JSONArray();
try {
Object ob;
while ((ob = jsparser.nextValue()) != null)
jos.put(ob);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jos;
}
记住:一个大括号的内容{ }就是一个JSONObject,中括号[ ]可以理解为一个JSONArray,JSONObject中具体某个属性可以用get("key")方法
具体的HTML请求跟JSON的解析如下
@OnClick(R.id.button1)
public void clickMethod(View view) {
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
"http://172.25.66.227:8080/Android/Test",
new RequestCallBack<String>() {
@Override
public void onLoading(long total, long current,
boolean isUploading) {
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
JSONArray jos = getJOS(responseInfo.result);
try {
JSONObject jo = (JSONObject) jos.get(0);
name.setText((String) jo.get("name"));
phone1.setText((CharSequence) ((JSONArray) jo
.get("phone")).get(0));
phone2.setText((CharSequence) ((JSONArray) jo
.get("phone")).get(1));
provice.setText((CharSequence) ((JSONObject) jo
.get("address")).get("provice"));
city.setText((CharSequence) ((JSONObject) jo
.get("address")).get("city"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onStart() {
}
@Override
public void onFailure(HttpException arg0, String arg1) {
}
});
}
注意:这里的Button事件由XUtils框架注入,具体见XUtils框架文档
其中网址是服务端的网址,顺便说一下,如果是安卓虚拟机访问本地Tomcat,本地电脑对于虚拟机的ip是10.0.0.2,这个很重要。真机的话自己找到电脑的ip吧,前提是对计算机网络要有一定了解,否则这交互没法做。
点击button,看服务端的结果是不是显示在手机上了。