物流json解析查询App

这是一个利用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);
        }
    });
}
 
效果如下

 

     

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值