关注分享主机优惠活动
国内外VPS云服务器

idea后台与Android数据的交互(IDEA Android sdk)

实现功能向后台发送数据,后台根据获取的数据查询数据库,并将相应的数据发送回客户端,以在界面开发工具中显示。客户端下面是需要创建或修改的文件,新手可以从客户端学习运行示例代码,显示登录用户名输入框和登录密码输入框。

实现功能:Android app向idEA后台发送ID数据,后台根据获取的ID数据查询数据库,并将相应的数据发送回客户端,在app界面显示。

开发工具:IDEA,Android studio,MySQL

Android端:(以下是初学者学习需要创建或修改的文件)Java:主活动来宾服务器布局:activity _ main build . gradle(app)and oid manifest . XML客户端运行示例:代码显示:

导入androidx . app compat . app . app compat activity;导入Android . OS . bundle;导入Android . OS . handler;导入Android . OS . message;导入Android . view . view;导入Android . widget . button;导入Android . widget . edittext;导入Android . widget . textview;导入Android . widget . toast;导入org . JSON . JSON exception;导入org . JSON . JSON object;导入Java . io . buffered reader;导入Java . io . file inputstream;导入Java . io . io exception;导入Java . io . inputstreamreader;公共类主活动扩展app compatibility {//登录用户名输入框私有EditText et _ username//登录密码输入框私有EditText et _ password//登录按钮私有EditText id私有按钮bt _ login私人TextView电视;@ Override protected void onCreate(Bundle savedInstanceState){ super . onCreate(savedInstanceState);setContentView(r . layout . activity _ main);//获取组件init();//点击登录按钮监控Bt _ log in . setonclicklistener(new view . onclick listener(){ @ override public void onclick(view v)){//toast。maketext(登录活动。这个,“登录成功!”,吐司。长度_长)。show();final Handler my Handler = new Handler(){ public void handle Message(Message msg){ String response result =(String)msg . obj;//吐司。maketext(登录活动。这个,“登录成功!”,吐司。长度_长)。show();//成功登录系统。out . println(" response "+响应结果);try { JSON object root = new JSON object(response result);string userName = root . getstring(" userName ");TV . append(" userName "+" = "+userName+"/n ");} catch(JSON exception e){ e . printstacktrace();} if(响应结果。equals ("+"true ")) {toast。make text(com . example . my application . main activity . this,“登录成功!“,干杯。长度_长)。show();}//登录失败else {toast。make text(com . example . my application . main activity . this,“登录失败!“,干杯。长度_长)。show();} } };new Thread(new Runnable(){ @ Override public void run(){ guest toserver guest toserver = new guest toserver();尝试{//如果您正在调用方法来验证GuestToServer中的用户名和密码,请使用下面的语句//String result = guest toserver . dopost(et _ username . gettext()。toString()。trim(),et_password.getText()。toString()。trim());string result = guettoserver . dopost(id . gettext()。toString()。trim());Message msg = new Message();msg.obj = resultmy handler . sendmessage(msg);} catch(io exception e){ e . printstacktrace();} } }).start();} });}/* * * Get component */private void init(){ et _ username =(edittext)findviewbyid(r . id . et _ username);et _ password =(EditText)findViewById(r . id . et _ password);id =(EditText)findViewById(r . id . id);Bt _ log in =(Button)findViewById(r . id . Bt _ log in);TV =(TextView)findViewById(r . id . TV);//获取TextView组件} } Guest to server导入Android . util . log;导入org . Apache . http . http entity;导入org . Apache . http . httpresponse;导入org . Apache . http . http status;import org . Apache . http . name value pair;导入org . Apache . http . client . http client;导入org . Apache . http . client . entity . urlencodedformentity;导入org . Apache . http . client . methods . http post;import org . Apache . http . impl . client . default http client;import org . Apache . http . message . basic name value pair;导入Java . io . buffered reader;导入Java . io . io exception;导入Java . io . inputstream;导入Java . io . inputstreamreader;导入Java . net . httpurl connection;导入Java . net . malformedurexception;导入Java . net . URL;导入Java . util . ArrayList;导入Java . util . list;公有类guest server {//localhost是本地主机的IP地址/login是idea后台项目名称私有字符串URL = " http://localhost:8080/log in ";//服务器返回的结果//String result = " ";/* * *使用Post向服务器发送请求并返回响应* *//如果使用验证用户名和密码的方法,使用公共字符串dopost (string username,String password)throwsioexception { *//并释放以下两个参数设置//* @ Parausername传递给服务器的用户名//* @ Parapassword传递给服务器的密码* @ param id id * @ return */publicstringdopost(stringid)throwsioexception { http client http client = new default http client();http post http post = new http post(URL);name value pair param 3 = new basic name value pair(" id ",id);list params = new ArrayList();params . add(param 3);//将参数包装在HttpEntity中,放入HttpPost的请求体中;HttpEntity HttpEntity = New UrlencodedforEntity(params," GBK ");http post . setentity(http entity);HttpResponse HttpResponse = http client . execute(http post);//如果响应成功,则if (httpresponse。getstatusline()。getstatuscode () = = httpstatus。sc _ ok){//获取消息体httpentity = httpresponse。get entity();InputStream InputStream = entity . get content();buffered reader br = new buffered reader(new InputStreamReader(inputStream));String readLine = nullwhile ((readLine = br.readLine())!= null){ result+= readLine;} inputstream . close();返回结果;}//响应失败else { return " false} } /*公共字符串doPost(字符串用户名,字符串密码)抛出io exception { http client http client = new default http client();http post http post = new http post(URL);//将用户名和密码参数加载到列表中;name value pair param 1 = new basic name value pair("用户名",用户名);name value pair param 2 = new basic name value pair(" password ",密码);list params = new ArrayList();params . add(param 1);params . add(param 2);//将参数包装在HttpEntity中,放入HttpPost的请求体中;HttpEntity HttpEntity = New UrlencodedforEntity(params," GBK ");http post . setentity(http entity);HttpResponse HttpResponse = http client . execute(http post);//如果响应成功,则if (httpresponse。getstatusline()。getstatuscode () = = httpstatus。sc _ ok){//获取消息体httpentity = httpresponse。get entity();InputStream InputStream = entity . get content();buffered reader br = new buffered reader(new InputStreamReader(inputStream));String readLine = nullwhile ((readLine = br.readLine())!= null){ result+= readLine;} inputstream . close();返回结果;}//响应失败else { return " false} }*/} activity_main.xml

如果项目中有错误,记得查看错误日志。一般apache不会在build.gradle中导入这一行由于版本不同,有些需要添加在manifest文件的附件中。

至此,Android app已经搭建完成,可以尝试运行IDEA后台项目:可以参考之前的小程序后台,共享一个后台和数据库,不需要改动。

未经允许不得转载:主机频道 » idea后台与Android数据的交互(IDEA Android sdk)

评论 抢沙发

评论前必须登录!