这是一个利用API接口通过Json解析查询物流的小实验
一、实验目的
1、掌握Json解析方式
2、熟悉API接口功能
3、掌握线程运作流程
二、实验环境
Window7+Android Stdio
三、实验内容与实验步骤(可附上截图和代码)
1、实验内容:利用API接口功能将物流信息通过Json解析出现,显示在APP上
2、实验步骤:
1) 设计界面
先在网上找一张合适的背景图,再利用线性布局设计界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bk"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快递公司"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_name"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="快递单号"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_num"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/send_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150px"
android:text="查询" />
<Button
android:id="@+id/quit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="160px"
android:text="退出" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/response_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
2)gardle添加依赖库
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:26.0.0-alpha1' compile 'com.squareup.okhttp3:okhttp:3.4.1' } 3 ) 编写主活动,设置点击事件protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText1=(EditText) findViewById(R.id.et_name);//快递公司 editText2=(EditText) findViewById(R.id.et_num);//快递单号 Button sendRequest = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response_text); sendRequest.setOnClickListener(this); } @Override public void onClick(View v) { if (v.getId() == R.id.send_request) { // sendRequestWithHttpURLConnection(); sendRequestWithOkHttp(); } }
4)编写点击事件
private void sendRequestWithOkHttp() { new Thread(new Runnable() { public void run() { try { String et1=editText1.getText().toString(); String et2=editText2.getText().toString(); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() // 指定访问的服务器地址是电脑本机 .url("http://www.kuaidi100.com/query?type="+et1+"&postid="+et2) .build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); JSONArray o=new JSONObject(responseData).getJSONArray("data"); String m=parseJSONWithJSONObject(o.toString()); showResponse(m); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
5)解析并显示Json
效果如下private String parseJSONWithJSONObject(String json) { StringBuilder sb=new StringBuilder(); try { JSONArray jsonArray = new JSONArray(json); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String time = jsonObject.getString("time"); String ftime = jsonObject.getString("ftime"); String context = jsonObject.getString("context"); String location = jsonObject.getString("location"); sb.append("\r\n"+"发出时间:"+time +"到达时间:"+ ftime+"\r\n" +context + location); System.out.print(sb); } } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } private void showResponse(final String response) { runOnUiThread(new Runnable() { @Override public void run() { // 在这里进行UI操作,将结果显示到界面上 responseText.setText(response); } }); }