91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android實現快遞單號查詢快遞狀態信息

發布時間:2020-10-11 10:27:12 來源:腳本之家 閱讀:355 作者:我的天空 我的夢 欄目:移動開發

今天介紹一個自己做的快遞單號查詢的簡單APP,供大家參考。由于需要使用http和json,本文在build.gradle(module:app)添加了okhttp3依賴和gson依賴。

dependencies { 
 compile fileTree(include: ['*.jar'], dir: 'libs') 
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
  exclude group: 'com.android.support', module: 'support-annotations' 
 }) 
 compile 'com.android.support:appcompat-v7:24.1.1' 
 testCompile 'junit:junit:4.12' 
 compile 'com.squareup.okhttp3:okhttp:3.6.0' 
 compile 'com.google.code.gson:gson:2.2.4' 
} 

看一下布局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 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" 
 android:orientation="vertical" 
 tools:context="com.yjp.deliverynoquerydemo.MainActivity"> 
 
 <Spinner 
  android:id="@+id/delivery_company_spinner" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_margin="5dp" 
  android:entries="@array/delivery_company"/> 
 
 <EditText 
  android:id="@+id/delivery_no_edit_text" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_margin="5dp" 
  android:hint="@string/please_enter_delivery_no" 
  android:inputType="number"/> 
 
 <Button 
  android:id="@+id/query_button" 
  android:layout_gravity="center" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_margin="5dp" 
  android:text="@string/query"/> 
 
 <ListView 
  android:id="@+id/messages_list_view" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_margin="5dp" 
  android:listSelector="@android:color/transparent"/> 
</LinearLayout> 

 ListView使用的item的布局

<?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"> 
 
 <TextView 
  android:id="@+id/time_text_view" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:textStyle="bold" 
  android:textAppearance="?android:textAppearanceMedium" 
  android:typeface="monospace"/> 
 
 <TextView 
  android:id="@+id/context_text_view" 
  android:layout_width="match_parent" 
  android:layout_height="50dp" 
  android:textAppearance="?android:textAppearanceSmall" 
  android:typeface="monospace"/> 
 
</LinearLayout> 

資源文件,首先是strings.xml

<resources> 
 <string name="app_name">快遞查詢</string> 
 <string name="please_enter_delivery_no">請輸入快遞單號</string> 
 <string name="query">查詢</string> 
 <string name="query_url">http://www.kuaidi100.com/query</string> 
</resources> 

這里我們使用了快遞100的接口,然后看看arrays.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <string-array name="delivery_company"> 
  <item>順豐</item> 
  <item>EMS</item> 
  <item>快捷</item> 
 </string-array> 
 
 <string-array name="delivery_company_id"> 
  <item>shunfeng</item> 
  <item>ems</item> 
  <item>kuaijiesudi</item> 
 </string-array> 
</resources> 

只做了3個快遞公司的查詢,還有很多其他的支持,界面如圖所示

Android實現快遞單號查詢快遞狀態信息

下面看看代碼,我們自定義一個Application類,主要通過資源,使用表驅動法動態構建一個快遞公司中文名與請求時候的公司編碼的映射表。

package com.yjp.deliverynoquerydemo.global; 
 
import android.app.Application; 
 
import com.yjp.deliverynoquerydemo.R; 
 
import java.util.HashMap; 
import java.util.Map; 
 
public class MyApplication extends Application { 
 
 private Map<String, String> mDeliveryCompanyTable = new HashMap<>(); 
 
 public String getDeliveryCompanyNo(String deliveryCompanyName) throws RuntimeException { 
 
  if (mDeliveryCompanyTable.isEmpty()) { 
   String[] names = getResources().getStringArray(R.array.delivery_company); 
   String[] ids = getResources().getStringArray(R.array.delivery_company_id); 
 
   if (names.length != ids.length) { 
    throw new RuntimeException(); 
   } 
 
   for (int i = 0; i < names.length; i++) { 
    mDeliveryCompanyTable.put(names[i], ids[i]); 
   } 
  } 
 
  return mDeliveryCompanyTable.get(deliveryCompanyName); 
 } 
} 

然后是模型,用來記錄獲取回來的快遞狀態信息

package com.yjp.deliverynoquerydemo.modal; 
 
import java.util.List; 
 
public class DeliveryMessages { 
 
 //派送單號 
 private String nu; 
 
 //快遞公司名稱 
 private String com; 
 
 //快遞信息 
 private List<Message> data; 
 
 //消息類 
 public static class Message { 
 
  //時間,格式為年-月-日 時:分:秒 
  private String time; 
 
  //詳細信息內容 
  private String context; 
 
  public String getTime() { 
   return time; 
  } 
 
  public void setTime(String time) { 
   this.time = time; 
  } 
 
  public String getContext() { 
   return context; 
  } 
 
  public void setContext(String context) { 
   this.context = context; 
  } 
 } 
 
 public String getNu() { 
  return nu; 
 } 
 
 public void setNu(String nu) { 
  this.nu = nu; 
 } 
 
 public String getCom() { 
  return com; 
 } 
 
 public void setCom(String com) { 
  this.com = com; 
 } 
 
 public List<Message> getData() { 
  return data; 
 } 
 
 public void setData(List<Message> data) { 
  this.data = data; 
 } 
} 

一個用來通過http獲取快遞信息的工具類,這里我們使用了okHttp3和gson

package com.yjp.deliverynoquerydemo.tools; 
 
import com.google.gson.Gson; 
import com.yjp.deliverynoquerydemo.modal.DeliveryMessages; 
 
import java.io.IOException; 
import java.net.SocketTimeoutException; 
import java.util.Map; 
import java.util.concurrent.TimeUnit; 
 
import okhttp3.Call; 
import okhttp3.Callback; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.Response; 
 
public class DeliveryMessageGetter { 
 
 //異步請求監聽接口 
 public interface DeliveryMessageGetterListener { 
  void onSuccess(DeliveryMessages deliveryMessages); 
  void onFailure(String errorStr); 
 } 
 
 //okHttp 
 private OkHttpClient mOkHttpClient = new OkHttpClient.Builder() 
   .readTimeout(10, TimeUnit.SECONDS) 
   .writeTimeout(10, TimeUnit.SECONDS) 
   .connectTimeout(10, TimeUnit.SECONDS) 
   .build(); 
 
 //異步GET請求 
 public void getAsync(final String url, 
       final Map<String, String> params, 
       final DeliveryMessageGetterListener listener) { 
 
  //構建請求URL 
  String requestString = url; 
  if (!params.isEmpty()) { 
   requestString += "?"; 
   for (Map.Entry<String, String> entry : params.entrySet()) { 
    requestString += entry.getKey() + "=" + entry.getValue() + "&"; 
   } 
   requestString = requestString.substring(0, requestString.length() - 1); 
  } 
 
  //創建一個Request 
  final Request request = new Request.Builder() 
    .url(requestString) 
    .build(); 
 
  //請求加入調度 
  Call call = mOkHttpClient.newCall(request); 
  call.enqueue(new Callback() { 
   @Override 
   public void onFailure(Call call, IOException e) { 
    if(e.getCause().equals(SocketTimeoutException.class)) { 
     listener.onFailure("查詢超時"); 
    } else { 
     listener.onFailure("查詢失敗"); 
    } 
   } 
 
   @Override 
   public void onResponse(Call call, Response response) throws IOException { 
    String messages = response.body().string(); 
    Gson gson = new Gson(); 
    DeliveryMessages deliveryMessages = gson.fromJson(messages, DeliveryMessages.class); 
 
    if (deliveryMessages != null) { 
     listener.onSuccess(deliveryMessages); 
    } else { 
     listener.onFailure("查詢失敗"); 
    } 
   } 
  }); 
 } 
 
} 

最后是我們的MainActivity

package com.yjp.deliverynoquerydemo; 
 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 
 
import com.yjp.deliverynoquerydemo.global.MyApplication; 
import com.yjp.deliverynoquerydemo.modal.DeliveryMessages; 
import com.yjp.deliverynoquerydemo.tools.DeliveryMessageGetter; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import static com.yjp.deliverynoquerydemo.modal.DeliveryMessages.Message; 
 
public class MainActivity extends AppCompatActivity implements DeliveryMessageGetter.DeliveryMessageGetterListener { 
 
 private List<Map<String, String>> mQueryData = new ArrayList<>(); 
 private SimpleAdapter mQueryAdapter; 
 
 private Spinner mDeliveryCompanySpinner; 
 private EditText mDeliveryNoEditText; 
 
 private ProgressDialog mQueryWaitDialog; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
 
  mDeliveryCompanySpinner = (Spinner) findViewById(R.id.delivery_company_spinner); 
  mDeliveryNoEditText = (EditText) findViewById(R.id.delivery_no_edit_text); 
  Button queryButton = (Button) findViewById(R.id.query_button); 
  ListView messagesListView = (ListView) findViewById(R.id.messages_list_view); 
 
  queryButton.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
 
    //沒有輸入快遞單號 
    if (0 == mDeliveryNoEditText.getText().length()) { 
     Toast.makeText(MainActivity.this, "請輸入快遞單號", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
 
    //創建ProgressDialog對象 
    mQueryWaitDialog = new ProgressDialog(MainActivity.this); 
    mQueryWaitDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    mQueryWaitDialog.setMessage("正在查詢..."); 
    mQueryWaitDialog.show(); 
 
    //準備請求參數 
    int selectedPosition = mDeliveryCompanySpinner.getSelectedItemPosition(); 
    String deliveryCompanyName = 
      getResources().getStringArray(R.array.delivery_company)[selectedPosition]; 
    Map<String, String> params = new HashMap<>(); 
    params.put("type", 
      ((MyApplication)getApplication()).getDeliveryCompanyNo(deliveryCompanyName)); 
    params.put("postid", mDeliveryNoEditText.getText().toString()); 
 
    //清空數據 
    mQueryData.clear(); 
 
    //發送請求 
    DeliveryMessageGetter getter = new DeliveryMessageGetter(); 
    getter.getAsync(getResources().getString(R.string.query_url), 
      params, MainActivity.this); 
   } 
  }); 
 
  mQueryAdapter = new SimpleAdapter(this, 
    mQueryData, 
    R.layout.query_list_item_layout, 
    new String[] {"time", "context"}, 
    new int[] {R.id.time_text_view, R.id.context_text_view}); 
  messagesListView.setAdapter(mQueryAdapter); 
 } 
 
 @Override 
 public void onSuccess(DeliveryMessages deliveryMessages) { 
  List<Message> messages = deliveryMessages.getData(); 
 
  for (Message message : messages) { 
   Map<String, String> map = new HashMap<>(); 
   map.put("time", message.getTime()); 
   map.put("context", message.getContext()); 
   mQueryData.add(map); 
  } 
 
  queryComplete("查詢完成"); 
 } 
 
 @Override 
 public void onFailure(String errorStr) { 
  final String hint = errorStr; 
  queryComplete("查詢失敗"); 
 } 
 
 private void queryComplete(final String toast) { 
  MainActivity.this.runOnUiThread(new Runnable() { 
   @Override 
   public void run() { 
    mQueryAdapter.notifyDataSetChanged(); 
    mQueryWaitDialog.dismiss(); 
    Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show(); 
   } 
  }); 
 } 
} 

主要是調用接口,實現功能,代碼比較好理解,不再贅述。最后給出Manifest文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 package="com.yjp.deliverynoquerydemo"> 
 
 <uses-permission android:name="android.permission.INTERNET" /> 
 
 <application 
  android:name=".global.MyApplication" 
  android:allowBackup="true" 
  android:icon="@mipmap/ic_launcher" 
  android:label="@string/app_name" 
  android:supportsRtl="true" 
  android:theme="@style/AppTheme"> 
  <activity android:name=".MainActivity" 
   android:windowSoftInputMode="stateHidden"> 
   <intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
 
    <category android:name="android.intent.category.LAUNCHER" /> 
   </intent-filter> 
  </activity> 
 </application> 
 
</manifest> 

主要是替換了默認的Application類,然后讓MainActivity默認不彈出軟鍵盤。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

荥阳市| 江西省| 柳江县| 商都县| 武鸣县| 济南市| 汝南县| 淮北市| 多伦县| 林芝县| 兰考县| 水城县| 桐庐县| 紫金县| 南安市| 宜良县| 泰安市| 隆化县| 合肥市| 甘洛县| 榆林市| 伊金霍洛旗| 保康县| 绥阳县| 金沙县| 三原县| 舟曲县| 延边| 阜城县| 包头市| 土默特右旗| 秭归县| 兰西县| 昂仁县| 嘉鱼县| 喀喇| 鹤峰县| 甘孜| 建水县| 临江市| 昭觉县|