Summary of Android client using phprpc protocol

Source: Internet
Author: User
Summary of implementing the Android client using the phprpc protocol. This article will be written after the Android version is complete ~ The result is delayed due to a series of issues. The following are some of my experiences with the problems I encountered when using the phprpc protocol to write android applications .? 1. Summary of Android client implementation using phprpc protocol on login machine

This article will be written after the Android version is completed ~ The result is delayed due to a series of things. The following are some of my experiences in compiling android applications using the phprpc protocol.

?

1. login mechanism and client synchronization problems

The server is a remote call interface provided by phprpc. of course, you must first enable Internet access for android:

?

 

?

? Insert the preceding statement in AndroidManifest. xml... This statement has affected the younger brother to check it for a long time... After the program was compiled, I couldn't display the content until I found out that this permission was missing. I wouldn't forget it if I killed it ~~

?

Then, the server uses the login authentication mode in the form of HashCode saved by session. That is to say, the client has always shared the same client object and HashCode. in Android, the Session or Cookie cross-page global variables cannot be used like in Web applications, for example, I wrote a blog post about global objects that can be included in Android. Create the following code:

?

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;}}

?

? The above is the public client object and HashCode, and change the following code in AndroidManifest. xml:

?


?

? The above android: name = ". ShareContext" statement declares a context global variable. For more information, see the blog post.

Initial login initialization context object:

?

Encryption context = (encryption context) getApplicationContext (); this. client = encryption context. getClient (); this. source = encryption context. 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 (), "verification error", Toast. LENGTH_SHORT ). show ();} else {client. useService (SERVICE_URL); using context. setClient (this. client); encryption context. setSource (this. source);} Object s = client. invoke ("selfuser_timeline", new Object [] {source, 0, 20}); Log. v ("source0", s. toString ());}

?

? Then the attributes of the context object can be retrieved from other activities:

?

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

? Then you can officially use them.

?

?

2. array serialization for remote call transmission

Although php-compiled hash code is transmitted in phprpc, after the client calls it, the callback is generally converted to an array that cannot be recognized by the existing system, the php server has transmitted an index array, but there is no index array on the java side. it is accurate to a processed HashMap, at the beginning, I still don't know that the phprpc database provides an array serialization and resolution library, and I can't get the values in it (the protocol library is quite good, but the api documentation seems a little worse ), to engage in phprpc, you just need to look at the source code and get the api or something. when you look at the source code, you will find that a library such as AssocArray comes with an index array class... Okay, continue...

Now the problem is solved. See the following code:

?

this.list = new ArrayList
 
  ();AssocArray alist = (AssocArray)client.invoke("home_timeline", new Object[]{this.source,0,20});for(int i = 0 ; i < alist.size();++i){AssocArray a = (AssocArray)alist.get(i);TopicInfo ti = new TopicInfo();ti.setNickname(Cast.toString(a.get("nickname")));ti.setUid(Cast.toString(a.get("uid")));ti.setCreateTime(Cast.toString(a.get("create_time")));ti.setAvatar(Cast.toString(a.get("avatar")));ti.setContent(Cast.toString(FunctionUtil.Html2Text(Cast.toString(a.get("content")))));this.list.add(ti);}
 

When remotely calling invoke, the returned array is directly formatted as the AssocArray class. next we can use the get method to retrieve data like using HashMap. this index array class does not inherit HashMap, instead, HashMap is used as an internal attribute.

That is to say, the phprpc library implements an outer encapsulation of the LinkedHashMap chain HashMap. the method in it is similar to HashMap. for details, see the source code.

?

3. remote call string formatting

This question seems to have been asked by many people on the Internet... After reading this api document, you have to correct it. In fact, after analyzing the source file source code, you will find that the original phprpc still provides the parsing class, which is Cast,

You can directly call the following static method:

?

Cast.toString(a.get("nickname"));

?

Summary: I feel that phprpc's api documentation is really not doing anything .. To be improved, I have to look at the source code of the library, but I feel that the use of phprpc on the android platform is no different from that in java. I am very upset about the loss of xml configuration at the beginning, the formatting of array strings is also tossing for a long time... Good and good...

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.