欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android后端服務(wù)器的搭建方法

 更新時(shí)間:2017年07月03日 09:51:40   作者:farley-fu  
本篇文章主要介紹了Android后端服務(wù)器的搭建方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一直做Android前端,今天突然心血來潮想搭建一個(gè)后臺(tái)玩玩。平時(shí)都是需要什么樣的接口直接出個(gè)接口文檔扔給后臺(tái)的兄弟,自己從來不操心他們內(nèi)部的實(shí)現(xiàn)問題。今天懷著好奇的心理去搭建了一個(gè)JAVA編譯環(huán)境下的后臺(tái)服務(wù)器。聽說用PHP搭建服務(wù)器的居多,但是我們做大Android的最熟悉的還是Java了,所以下面我就開始搭建這個(gè)服務(wù)器。很簡單。。。

首先我下載了一個(gè)myelipse應(yīng)為我們開發(fā)android的eclipse不能創(chuàng)建web project 要不然你去下載個(gè)插件也行,下載好以后創(chuàng)建web project會(huì)生成一個(gè)目錄,然后右鍵你的這個(gè)項(xiàng)目選擇myeclipse  -> add structs capabilities... 選擇2.1 finish  OK這樣就創(chuàng)建成功這個(gè)項(xiàng)目了,下面我貼出來我的項(xiàng)目樹供大家參考(感謝yayun0516 ,他的博文給了我很大的幫助,但是其中有些不足我已經(jīng)在下面改正了)

下面配置structs.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package name="struts2" extends="struts-default" namespace="/">
    <action name="getjson" class="com.shao.action.JSONAction"
      method="json">
      <result name="success">index.jsp</result>
    </action>
  </package>
</struts> 

只有這一個(gè)需要配置,其他的在你添加struct的時(shí)候就會(huì)自動(dòng)生成。下面創(chuàng)建類型文件

package com.shao.domain;

public class Music {  
  
  private Integer id;   
  private String name;   
  private String time;  
  private String author;  
  public Integer getId() {  
    return id;  
  }  
  public void setId(Integer id) {  
    this.id = id;  
  }  
  public String getName() {  
    return name;  
  }  
  public void setName(String name) {  
    this.name = name;  
  }  
  public String getTime() {  
    return time;  
  }  
  public void setTime(String time) {  
    this.time = time;  
  }  
  public String getAuthor() {  
    return author;  
  }  
  public void setAuthor(String author) {  
    this.author = author;  
  }   
}  

然后再創(chuàng)建轉(zhuǎn)json的方法JSONAction:

package com.shao.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.shao.domain.Music;

public class JSONAction extends ActionSupport implements ServletRequestAware,
    ServletResponseAware {

  /** 
   *  
   */
  private static final long serialVersionUID = -3604892179657815531L;
  private HttpServletRequest request;
  private HttpServletResponse response;
  private String format;

  public String getFormat() {
    return format;
  }

  public void setFormat(String format) {
    this.format = format;
  }

  @Override
  public void setServletRequest(HttpServletRequest request) {
    this.request = request;
  }

  @Override
  public void setServletResponse(HttpServletResponse response) {
    this.response = response;
  }

  public void json() {
    List<Music> list = new ArrayList<Music>();
    Gson gson = new Gson();
    Music m1 = new Music();
    m1.setId(1);
    m1.setAuthor("周");
    m1.setName("外婆");
    m1.setTime("04:04");
    list.add(m1);
    Music m2 = new Music();
    m2.setId(2);
    m2.setAuthor("周杰倫");
    m2.setName("半獸人");
    m2.setTime("04:05");
    list.add(m2);
    Music m3 = new Music();
    m3.setId(3);
    m3.setAuthor("周杰倫");
    m3.setName("烏克麗麗");
    m3.setTime("02:55");
    list.add(m3);
    java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Music>>() {
    }.getType(); // 指定type
    String beanListToJson = gson.toJson(list, type); // list轉(zhuǎn)換成json字符串
    System.out.println("GSON-->" + beanListToJson);
    try {
      response.setContentType("application/json; charset=GBK");
      response.setCharacterEncoding("UTF-8");
      this.response.getWriter().write(beanListToJson);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 response.setContentType("application/json; charset=GBK");一定要注意,如果不加這句會(huì)在你請求服務(wù)器數(shù)據(jù)的時(shí)候,中文出現(xiàn)亂碼現(xiàn)象,同時(shí)在index.jsp中加入了contentType="text/html; charset=GBK"
還有不要忘了導(dǎo)入Gson包。

完了,就這樣服務(wù)器就完成了,下面運(yùn)行一下 run as -> myeclipse service application 成功后會(huì)彈出一個(gè)框,上面寫著This is my JSP page.這就說明你已經(jīng)成功創(chuàng)建了服務(wù)器。

下面打開http://localhost:8080/Test2/getjson.action 下面就是服務(wù)器返回的內(nèi)容了。

基本就是這樣了,又不懂的可以問我。下面說android端的,更簡單了。

創(chuàng)建我們的項(xiàng)目然后加入xutils和gson包。

這是一個(gè)新建的項(xiàng)目,在activity_main.xml中我給那個(gè)TextView添加了一個(gè)id

然后在MainActivity中實(shí)現(xiàn)如下:

package com.example.test2;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView text = (TextView) findViewById(R.id.text);
    HttpUtils httpUtils = new HttpUtils();
    httpUtils.send(HttpMethod.POST, "http://192.168.199.171:8080/Test2/getjson.action", new RequestCallBack<String>() {

      public void onFailure(HttpException arg0, String arg1) {
        Log.d("=====================onFailure", arg1+";"+arg0.toString());
        
      }

      public void onSuccess(ResponseInfo<String> arg0) {
        Log.d("=====================onSuccess", arg0.result);
        text.setText(arg0.result);
      }
    
      
    });
  }


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }
}

整個(gè)android端就是這樣了,下面我們運(yùn)行一下剛才的數(shù)據(jù)已經(jīng)顯示在了該TextView上。

 其實(shí)整個(gè)代碼內(nèi)容是很簡單的,主要難的地方就是在環(huán)境搭建上,大家多練練吧,整個(gè)代碼是我跑下來的,所以代碼沒有問題,如果你跑不成功就多去研究研究環(huán)境搭建。

分享至此,以后可以往這方面多了解一下,就算不做后臺(tái)開發(fā),也要多了解了解,減少溝通成本。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論