suchen
HeimBackend-EntwicklungPHP-Tutorial运用phprpc协议实现Android客户端的一些总结

应用phprpc协议实现Android客户端的一些总结

本来这篇文章会放Android版本做完就写的~结果由于一系列的事情而耽搁掉了,下面是我在使用phprpc协议编写android应用时出现的问题的一些心得总结。

?

1、登陆机制及客户端同步问题

服务端是由phprpc提供的远程调用接口,当然首先要开启android的互联网访问权限:

?

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

?

? 在AndroidManifest.xml插入以上语句,哎。。。这个语句害小弟查了半天啊。。。程序编好居然一直没法显示内容一查才知道少了这个权限,打死我也不会忘了它~~

?

接着因为服务端用的是会话保存HashCode形式的登录验证模式。就是说在客户端中一直公用同一个客户端对象和HashCode,在Android不能像Web应用一样使用Session或Cookie跨页面全局变量,如以前有一篇我写的博文写的可以用Android自带的全局对象。创建如下代码:

?

import org.phprpc.PHPRPC_Client;import android.app.Application;public class ShareContext extends Application {	private PHPRPC_Client client = null;	private String source = null;	public String getSource() {		return source;	}	public void setSource(String source) {		this.source = source;	}	public PHPRPC_Client getClient() {		return client;	}	public void setClient(PHPRPC_Client client) {		this.client = client;	}}

?

? 上面就是公用的客户端对象和HashCode,并在AndroidManifest.xml中更改下列代码:

?

<application android:name=".ShareContext" android:icon="@drawable/icon" android:label="@string/app_name"></application>

?

? 上面的android:name=".ShareContext"这句话就是声明一个context上下文全局变量,再详细的参见那篇博文。

初次登陆初始化上下文对象:

?

shareContext = ((ShareContext)getApplicationContext());		this.client = shareContext.getClient();		this.source = shareContext.getSource();				if(this.client == null){						Intent it = getIntent();	        String accountStr = it.getStringExtra("accountStr").toString();	        String passwordStr = it.getStringExtra("passwordStr").toString();	        	        this.client = new PHPRPC_Client(SERVICE_URL);  	        this.client.setEncryptMode(2);  	        	        this.source = Cast.toString(client.invoke("check_login", new Object[]{accountStr,passwordStr}));	        	        if(this.source == null){	        	Log.e("flowg_error", "source not find!");	        	Toast.makeText(getApplicationContext(), "验证错误",Toast.LENGTH_SHORT).show();	        }else{	        	client.useService(SERVICE_URL);		        shareContext.setClient(this.client);		        shareContext.setSource(this.source);	        }	        	        Object s = client.invoke("selfuser_timeline", new Object[]{source,0,20});			Log.v("source0",s.toString());	       		}

?

? 之后就可以在其他Activity中取出上下文对象中这两个属性了:

?

	shareContext = ((ShareContext)getApplicationContext());    	client = shareContext.getClient();    	source = shareContext.getSource();

? 在其后就能正式的使用它们了。

?

?

2、关于远程调用传输的数组序列化问题

在phprpc中虽然传输的是php编译化hash码,但客户端调用后回调中的一般会转化为现有系统无法识别的数组,对于php服务端传输过了是一个索引数组,而在java端是没有索引数组这一说的,准确是一个经过处理的HashMap,刚开始还不知道phprpc库有提供数组序列化解析库,一直取不出里面的值(协议库相当不错,但api文档做的貌似差了点),搞phprpc就是看源码来弄,api什么的只是入个门,在看源码时候发现有AssocArray这样一个库自带索引数组类。。。好吧,继续。。。

现在问题就解决了,看下面代码:

?

this.list = new ArrayList<topicinfo>();AssocArray alist = (AssocArray)client.invoke("home_timeline", new Object[]{this.source,0,20});for(int i = 0 ; i <p>在对于invoke远程调用时返回数组直接格式化为<span style="white-space: pre;">AssocArray类,接下来就能像使用HashMap一样使用get方法取出数据了,这个索引数组类并不是继承HashMap,而是将HashMap作为内部属性</span></p>
<p>,就是说phprpc库对LinkedHashMap链式HashMap做了一层外层封装,里面的方法基本和HashMap差不多,具体可以看源码。</p>
<p>?</p>
<p><span style="white-space: pre;">	</span>3、远程调用字符串的格式化</p>
<p><span style="white-space: pre;">	</span>这个问题貌似在网上有很多人提问。。。看了这个api文档真的得修正修正了,其实分析了源文件源码后会发现原来phprpc还是提供解析类,这个类就是Cast,</p>
<p>直接调用下面的静态方法便可以搞定:</p>
<p>?</p>
<pre name="code" class="java">Cast.toString(a.get("nickname"));

?

小结:感觉phprpc的api文档真的做的不咋的。。有待完善,害得我有问题就要看库源码,不过感觉android平台下使用phprpc应该和java下没有什么区别,刚开始的xml配置丢失搞的我很懊恼啊,而其中的格式化数组字符串什么的也折腾了好久。。。善哉善哉。。。

Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
在Android中实现轮询的方法是什么?在Android中实现轮询的方法是什么?Sep 21, 2023 pm 08:33 PM

Android中的轮询是一项关键技术,它允许应用程序定期从服务器或数据源检索和更新信息。通过实施轮询,开发人员可以确保实时数据同步并向用户提供最新的内容。它涉及定期向服务器或数据源发送请求并获取最新信息。Android提供了定时器、线程、后台服务等多种机制来高效地完成轮询。这使开发人员能够设计与远程数据源保持同步的响应式动态应用程序。本文探讨了如何在Android中实现轮询。它涵盖了实现此功能所涉及的关键注意事项和步骤。轮询定期检查更新并从服务器或源检索数据的过程在Android中称为轮询。通过

如何在Android中实现按下返回键再次退出的功能?如何在Android中实现按下返回键再次退出的功能?Aug 30, 2023 am 08:05 AM

为了提升用户体验并防止数据或进度丢失,Android应用程序开发者必须避免意外退出。他们可以通过加入“再次按返回退出”功能来实现这一点,该功能要求用户在特定时间内连续按两次返回按钮才能退出应用程序。这种实现显著提升了用户参与度和满意度,确保他们不会意外丢失任何重要信息Thisguideexaminesthepracticalstepstoadd"PressBackAgaintoExit"capabilityinAndroid.Itpresentsasystematicguid

Android逆向中smali复杂类实例分析Android逆向中smali复杂类实例分析May 12, 2023 pm 04:22 PM

1.java复杂类如果有什么地方不懂,请看:JAVA总纲或者构造方法这里贴代码,很简单没有难度。2.smali代码我们要把java代码转为smali代码,可以参考java转smali我们还是分模块来看。2.1第一个模块——信息模块这个模块就是基本信息,说明了类名等,知道就好对分析帮助不大。2.2第二个模块——构造方法我们来一句一句解析,如果有之前解析重复的地方就不再重复了。但是会提供链接。.methodpublicconstructor(Ljava/lang/String;I)V这一句话分为.m

如何在2023年将 WhatsApp 从安卓迁移到 iPhone 15?如何在2023年将 WhatsApp 从安卓迁移到 iPhone 15?Sep 22, 2023 pm 02:37 PM

如何将WhatsApp聊天从Android转移到iPhone?你已经拿到了新的iPhone15,并且你正在从Android跳跃?如果是这种情况,您可能还对将WhatsApp从Android转移到iPhone感到好奇。但是,老实说,这有点棘手,因为Android和iPhone的操作系统不兼容。但不要失去希望。这不是什么不可能完成的任务。让我们在本文中讨论几种将WhatsApp从Android转移到iPhone15的方法。因此,坚持到最后以彻底学习解决方案。如何在不删除数据的情况下将WhatsApp

同样基于linux为什么安卓效率低同样基于linux为什么安卓效率低Mar 15, 2023 pm 07:16 PM

原因:1、安卓系统上设置了一个JAVA虚拟机来支持Java应用程序的运行,而这种虚拟机对硬件的消耗是非常大的;2、手机生产厂商对安卓系统的定制与开发,增加了安卓系统的负担,拖慢其运行速度影响其流畅性;3、应用软件太臃肿,同质化严重,在一定程度上拖慢安卓手机的运行速度。

Android中动态导出dex文件的方法是什么Android中动态导出dex文件的方法是什么May 30, 2023 pm 04:52 PM

1.启动ida端口监听1.1启动Android_server服务1.2端口转发1.3软件进入调试模式2.ida下断2.1attach附加进程2.2断三项2.3选择进程2.4打开Modules搜索artPS:小知识Android4.4版本之前系统函数在libdvm.soAndroid5.0之后系统函数在libart.so2.5打开Openmemory()函数在libart.so中搜索Openmemory函数并且跟进去。PS:小知识一般来说,系统dex都会在这个函数中进行加载,但是会出现一个问题,后

iOS 16.2 引入“自定义辅助功能模式”,为 iPhone 和 iPad 提供简化的体验iOS 16.2 引入“自定义辅助功能模式”,为 iPhone 和 iPad 提供简化的体验Apr 13, 2023 am 11:07 AM

苹果公司周二向开发人员发布了iOS 16.2 beta 2,因为该公司准备在 12 月向公众提供更新。正式地,它添加了新的 Freeform 协作应用程序和对 Home 应用程序的改进。在后台,9to5Mac发现 Apple 一直在开发一种新的“自定义辅助功能模式”,该模式将为 iPhone 和 iPad 提供“流线型”体验。自定义辅助功能模式这种代号为“Clarity”的新模式基本上用更精简的模式取代了 Springboard(这是 iOS 的主要界面)。该功能在当前测试版中仍对用户不可用,将

如何解决在Ubuntu下无法删除Android模拟器并出现错误提示的问题?如何解决在Ubuntu下无法删除Android模拟器并出现错误提示的问题?Dec 31, 2023 am 09:19 AM

在Ubuntu系统中,很多用户由于测试需要会多建几个模拟器,为了不占用系统内存资源,在完成测试后就把这些不用的模拟器删掉,但是在删除过程中出现无法删除的问题,系统提示“theandroid“XXX”virtuledeviceiscurrently...”,并显示正在使用不能删除,遇到这个问题该怎么办呢?别担心!下面小编就为大家带来Ubuntu下删除模拟器失败提示“theandroid“XXX”virtule的解决办法!一起

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heiße Werkzeuge

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

MantisBT

MantisBT

Mantis ist ein einfach zu implementierendes webbasiertes Tool zur Fehlerverfolgung, das die Fehlerverfolgung von Produkten unterstützen soll. Es erfordert PHP, MySQL und einen Webserver. Schauen Sie sich unsere Demo- und Hosting-Services an.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) ist eine PHP/MySQL-Webanwendung, die sehr anfällig ist. Seine Hauptziele bestehen darin, Sicherheitsexperten dabei zu helfen, ihre Fähigkeiten und Tools in einem rechtlichen Umfeld zu testen, Webentwicklern dabei zu helfen, den Prozess der Sicherung von Webanwendungen besser zu verstehen, und Lehrern/Schülern dabei zu helfen, in einer Unterrichtsumgebung Webanwendungen zu lehren/lernen Sicherheit. Das Ziel von DVWA besteht darin, einige der häufigsten Web-Schwachstellen über eine einfache und unkomplizierte Benutzeroberfläche mit unterschiedlichen Schwierigkeitsgraden zu üben. Bitte beachten Sie, dass diese Software