SSH server and Android communication (1) -- server, sshandroid

Source: Internet
Author: User

SSH server and Android communication (1) -- server, sshandroid

Many applications require that the SSH server not only communicate with the PC, but also with Android mobile devices. JSON is required. The basic principle is that the server converts the data to JSON format and sends it to the Android client. The client then parses the data in JSON format to implement communication between the SSH server and the Android client.

(1) create a User library that contains the jar required by JSON:

Note that in addition to adding Build path, gson also needs to be copied to the WEB-INF/lib/; otherwise, an error will occur.

(2) create a ResUtil file and convert the data to a JSON string using GSON:

The Code is as follows:

package com.school.tool;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;public class ResUtil {        public static void toJson(HttpServletResponse response, Object data)         throws IOException {                Gson gson = new Gson();        String result = gson.toJson(data);        response.setContentType("text/json; charset=utf-8");        response.setHeader("Cache-Control", "no-cache");        PrintWriter out = response.getWriter();        out.print(result);        out.flush();        out.close();            }  }

(3) create an AClasQueryAction file to process requests from the Android client:

The Code is as follows:

Package com. school. action; import java. io. IOException; import java. util. hashMap; import java. util. list; import java. util. map; import org. apache. struts2.ServletActionContext; import net. sf. json. JSONArray; import com. school. entity. clas; import com. school. service. clasService; import com. school. tool. resUtil; import com. opensymphony. xwork2.ActionSupport; @ SuppressWarnings ("serial") public class AClasQueryAction extends ActionSupport {private ClasService clasService; public void setClasService (ClasService clasService) {this. clasService = clasService;} private Clas clas; // encapsulate and return all course information public String execute () {Map <String, Object> map = new HashMap <String, object> (); List <Clas> claslist = clasService. findAll (); try {map. put ("Classes", claslist); ResUtil. toJson (ServletActionContext. getResponse (), map);} catch (IOException e) {e. printStackTrace ();} return null ;}}

This file obtains all the course information, encapsulates it in Map, and returns it to the client.

(4) modify the applicationContext. xml and struts. xml files and add the following statements to applicationContext. xml:

    <bean id="aClasQueryAction" class="com.school.action.AClasQueryAction">         <property name="clasService" ref="clasService"></property>    </bean>

Add the following statement to the struts. xml file:

        <action name="aclasquery" class="aClasQueryAction">        </action>

(5) restart the server and access http: // localhost: 8080/School/aclasquery in the browser. The result is as follows:

The server correctly returns the data of all courses in JSON format. If you use the Android client to access this URL, you can also obtain the above JSON format data, and then parse it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.