最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 服務(wù)端將位置信息發(fā)送給客戶(hù)端的實(shí)現(xiàn)

 更新時(shí)間:2021年01月25日 09:50:56   作者:歐陽(yáng)子遙  
這篇文章主要介紹了Android 服務(wù)端將位置信息發(fā)送給客戶(hù)端的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、問(wèn)題

Android 服務(wù)端將位置信息發(fā)送給客戶(hù)端

二、環(huán)境

AndroidStudio Eclipse

三、代碼實(shí)現(xiàn)

服務(wù)端Servlet調(diào)用Dao層在數(shù)據(jù)庫(kù)中查找數(shù)據(jù),在servlet中將查找到的數(shù)據(jù)匯集成json字符串(json數(shù)組形式)。

服務(wù)端:

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setContentType("text/plain; charset=UTF-8");
 request.setCharacterEncoding("UTF-8");
 ServerToParentDao stpDao = new ServerToParentDao();
// String num = mtpDao.query();
// System.out.println(num);
 PrintWriter out = response.getWriter();
 StringBuffer sb = new StringBuffer();
 sb.append('[');
 List<Address> addrList = stpDao.queryOne();
 for (Address address : addrList) {
  sb.append('{').append("\"id\":").append("" + address.getId() + "").append(",");
  sb.append("\"latitude\":").append("\"" + address.getLatitude() + "\"").append(",");
  sb.append("\"longitude\":").append("\"" + address.getLongitude() + "\"").append(",");
  sb.append("\"time\":\"").append(address.getTime());
  sb.append("\"}").append(",");
 }
 sb.deleteCharAt(sb.length() - 1);
 sb.append(']');
 out.write(sb.toString());
 System.out.println(sb.toString());
// request.setAttribute("json",sb.toString());
// request.getRequestDispatcher("watch.jsp").forward(request, response);
// out.write(num);
//  response.getOutputStream().write(mtpDao.query().getBytes("UTF-8"));
 out.flush();
 out.close();

// System.err.println(request.getParameter(""));
//  System.out.println(code);
 System.out.println("連接成功");
// PrintWriter printWriter = response.getWriter();
// printWriter.print("客戶(hù)端你好,數(shù)據(jù)連接成功!");
// printWriter.flush();
// printWriter.close();
 }

客戶(hù)端:

sendButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        HttpPost httpRequest = new HttpPost("http://192.168.159.1:8080/MyAndroidServer/ServerToParentServlet");
        List<NameValuePair> params = new ArrayList<NameValuePair>();
//        String str = "1";
//        params.add(new BasicNameValuePair("Code", str));
        Log.i("MY3", "Has Done");
        try {
//          httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//設(shè)置請(qǐng)求參數(shù)項(xiàng)
          HttpClient httpClient = new DefaultHttpClient();
          HttpResponse httpResponse = httpClient.execute(httpRequest);//執(zhí)行請(qǐng)求返回響應(yīng)
          if (httpResponse.getStatusLine().getStatusCode() == 200) {//判斷是否請(qǐng)求成功
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
              System.out.println("---------");
//              System.out.println("Respone content" + EntityUtils.toString(entity, "UTF-8"));
              Intent intent = new Intent(ParentRequest.this,MainActivity.class);
              intent.putExtra("jsonString",EntityUtils.toString(entity, "UTF-8"));
              startActivity(intent);
            }
        Log.i("MY2", "Has Done");
          } else {
            Toast.makeText(ParentRequest.this, "沒(méi)有獲取到Android服務(wù)器端的響應(yīng)!", Toast.LENGTH_LONG).show();
          }
        } catch (ClientProtocolException e) {
          e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });

請(qǐng)求地址書(shū)寫(xiě)形式:http://主機(jī)IP地址:端口號(hào)/項(xiàng)目名/action名

HttpPost方式建立連接,HttpResponse.getEntity()獲取響應(yīng)信息,EntityUtils.toString(entity, “UTF-8”)將entity轉(zhuǎn)為String字符串,Intent將JSON字符串傳遞到其他activity頁(yè)面中去。

JSON字符串解析類(lèi):

public static List<Address> getAddress(String jsonStr)
      throws JSONException {
    /******************* 解析 ***********************/
    // 初始化list數(shù)組對(duì)象
    List<Address> mList = new ArrayList<Address>();
    Address address = new Address();
    JSONArray array = new JSONArray(jsonStr);
    for (int i = 0; i < array.length(); i++) {
      JSONObject jsonObject = array.getJSONObject(i);
      address = new Address(jsonObject.getInt("id"),
          jsonObject.getString("latitude"), jsonObject.getString("longitude"),
          jsonObject.getString("time"));
      mList.add(address);
    }
    return mList;
  }

我這個(gè)是當(dāng)時(shí)在做一個(gè)兒童定位寫(xiě)的,數(shù)據(jù)庫(kù)設(shè)計(jì)沒(méi)思考全面,思維比較狹隘。

應(yīng)該思考到的是兒童信息表中兒童信息要跟父母表中父母信息對(duì)應(yīng)起來(lái),即這APP是給多對(duì)父母和孩子使用的,而不是一對(duì)父母與孩子。

服務(wù)端也不應(yīng)該是使用本地的,應(yīng)該使用云服務(wù)器,這樣就不會(huì)被同一局域網(wǎng)所限制。

Android 客戶(hù)端將位置信息發(fā)送給服務(wù)端

代碼實(shí)現(xiàn)

客戶(hù)端:

 HttpPost httpRequest = new HttpPost("http://192.168.159.1:8080/MyAndroidServer/ChildrenToServerServlet");
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm");
      Date date = new Date(System.currentTimeMillis());
      String str=simpleDateFormat.format(date);
      System.out.println(str);
      params.add(new BasicNameValuePair("Time", str));
      params.add(new BasicNameValuePair("Latitude",latitude));
      params.add(new BasicNameValuePair("Longitude", longitude));
      try {
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//設(shè)置請(qǐng)求參數(shù)項(xiàng)
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpRequest);//執(zhí)行請(qǐng)求返回響應(yīng)
        if(httpResponse.getStatusLine().getStatusCode() == 200){//判斷是否請(qǐng)求成功
//          Toast.makeText(ChildrenToServerActivity.this, EntityUtils.toString(httpResponse.getEntity()), Toast.LENGTH_LONG).show();
          Intent intent = new Intent();
          intent.setAction("cn.abel.action.broadcast");
          intent.putExtra("Response", EntityUtils.toString(httpResponse.getEntity()));
          context.sendBroadcast(intent);
        }else{
//          Toast.makeText(MainActivity.this, "沒(méi)有獲取到Android服務(wù)器端的響應(yīng)!", Toast.LENGTH_LONG).show();
          Intent intent = new Intent();
          intent.setAction("cn.abel.action.broadcast");
          intent.putExtra("Response", "沒(méi)有獲取到Android服務(wù)器端的響應(yīng)!");
          context.sendBroadcast(intent);
        }
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
params.add(new BasicNameValuePair(“Time”, str));

Time是str的變量名,用于服務(wù)端接收數(shù)據(jù)用的。
這是用來(lái)添加要傳遞給服務(wù)端的數(shù)據(jù),為String字符串形式。

服務(wù)端:

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 response.setContentType("text/plain; charset=UTF-8");
 request.setCharacterEncoding("UTF-8");
 String time = request.getParameter("Time");
 String latitude = request.getParameter("Latitude");
 String longitude = request.getParameter("Longitude");
 ChildrenToAddressDao addressDao = new ChildrenToAddressDao();
 addressDao.insert(latitude, longitude, time);
 
 System.err.println(request.getParameter("Time"));
 System.err.println(request.getParameter("Latitude"));
 System.err.println(request.getParameter("Longitude"));
 PrintWriter printWriter = response.getWriter();
 printWriter.print("客戶(hù)端你好,數(shù)據(jù)連接成功!");
 printWriter.flush();
 printWriter.close();
 }

request.getParameter(“變量名”)是用來(lái)接收客戶(hù)端對(duì)應(yīng)變量名的數(shù)據(jù)。
addressDao.insert()是我自己定義的方法,將接收到的數(shù)據(jù)存入MySQL中。

到此這篇關(guān)于Android 服務(wù)端將位置信息發(fā)送給客戶(hù)端的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android 服務(wù)端位置信息發(fā)送給客戶(hù)端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Kotlin Suspend掛起函數(shù)的使用詳解

    Kotlin Suspend掛起函數(shù)的使用詳解

    這里介紹 Kotlin Suspend 掛起函數(shù)的使用。掛起(suspend)函數(shù)是所有協(xié)程的核心。 掛起函數(shù)可以執(zhí)行長(zhǎng)時(shí)間運(yùn)行的操作并等待它完成而不會(huì)阻塞主線程。Kotlin 的 suspend 關(guān)鍵字可以幫助我們消除回調(diào),用同步的寫(xiě)法寫(xiě)異步
    2023-02-02
  • Android WebView開(kāi)發(fā)之WebView與Native交互

    Android WebView開(kāi)發(fā)之WebView與Native交互

    隨著H5的廣泛使用,Android開(kāi)發(fā)過(guò)程中免不了會(huì)使用網(wǎng)頁(yè)來(lái)做展示,那么web與native之間的通信就顯得尤其重要了,其實(shí)際上是JavaScript與java之間的通信。本文將為大家詳細(xì)介紹二者是如何實(shí)現(xiàn)交互的,需要的朋友可以參考一下
    2021-12-12
  • Android ANR(Application Not Responding)的分析

    Android ANR(Application Not Responding)的分析

    這篇文章主要介紹了Android ANR(Application Not Responding)的分析的相關(guān)資料,這里說(shuō)明什么原因出現(xiàn)應(yīng)用程序的強(qiáng)制關(guān)閉,并說(shuō)明該如何避免,需要的朋友可以參考下
    2017-08-08
  • 教你輕松制作Android音樂(lè)播放器

    教你輕松制作Android音樂(lè)播放器

    這篇文章主要教大家輕松制作Android音樂(lè)播放器,制作一款屬于自己的Android音樂(lè)播放器,希望大家喜歡。
    2015-11-11
  • Android實(shí)現(xiàn)按鈕拖拽還原功能

    Android實(shí)現(xiàn)按鈕拖拽還原功能

    這篇文章主要介紹了Android按鈕拖拽還原功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 微信公眾平臺(tái)開(kāi)發(fā)入門(mén)教程(SAE方倍工作室)

    微信公眾平臺(tái)開(kāi)發(fā)入門(mén)教程(SAE方倍工作室)

    在這篇微信公眾平臺(tái)開(kāi)發(fā)教程中,我們假定你已經(jīng)有了PHP語(yǔ)言程序、MySQL數(shù)據(jù)庫(kù)、計(jì)算機(jī)網(wǎng)絡(luò)通訊、及HTTP/XML/CSS/JS等基礎(chǔ)
    2014-05-05
  • 使用AndroidStudio上傳忽略文件至SVN Server的解決辦法

    使用AndroidStudio上傳忽略文件至SVN Server的解決辦法

    這篇文章主要介紹了使用AndroidStudio上傳忽略文件至SVN Server的解決辦法 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android實(shí)現(xiàn)使用微信登錄第三方APP的方法

    Android實(shí)現(xiàn)使用微信登錄第三方APP的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)使用微信登錄第三方APP的方法,結(jié)合實(shí)例形式分析了Android微信登錄APP的操作步驟與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-11-11
  • Android生成帶圓角的Bitmap圖片

    Android生成帶圓角的Bitmap圖片

    這篇文章主要介紹了Android生成帶圓角的Bitmap圖片,涉及Android通過(guò)Canvas實(shí)現(xiàn)繪制帶圓角的圖片相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Android設(shè)計(jì)模式之單例模式解析

    Android設(shè)計(jì)模式之單例模式解析

    這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)模式之單例模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評(píng)論

望奎县| 马龙县| 天门市| 通海县| 乌拉特中旗| 望奎县| 六安市| 荣昌县| 榆中县| 长宁区| 罗源县| 安溪县| 龙江县| 平南县| 广东省| 鄱阳县| 长宁区| 鄂伦春自治旗| 武鸣县| 阜康市| 河间市| 铁岭县| 米脂县| 丰都县| 旌德县| 凤凰县| 平阳县| 温宿县| 芷江| 河津市| 扎兰屯市| 烟台市| 尖扎县| 栾川县| 明溪县| 错那县| 河津市| 炎陵县| 尤溪县| 平乐县| 甘孜|