分享

Developing a shared Android / iOS network library

 quasiceo 2015-03-15

I have been primarily developing iOS apps but now, I'm developing applications for both iOS and Android. As much as possible, I'm trying to write code that is platform-independent as possible.

What have people done for networking functionality? I can't seem to find any resources on making GET/POST HTTP requests in Android.

How would you approach the issue of developing a shared networking library for both iOS and Android.

EDIT: I'll be doing all my Android network programming in C/C++ and not Java!

Are there any C/C++ networking libraries for Android (and subsequently can be used for iOS)?

asked Dec 19 '10 at 23:06
Mark
1,06122449

    
Uh, not being clear on what you're trying to achieve, I just like to ask: You are aware that there are already several attempts at this task, providing a x-platform framework, such as letting you write your apps in html+js, then building them into a custom app, one for each platform? –  Thomas Tempelmann Dec 19 '10 at 23:13
    
related question : /questions/3045477/… –  Guido García Dec 19 '10 at 23:15

6 Answers

up vote 8 down vote accepted

I don't think this is as impossible as most people here make it sound like. I interviewed someone recently who worked for a large mobile game development company and he mentioned that they do as much work as they can in C/C++ libs in order to reuse the code on multiple platforms including Android and iOS. I haven't tried this myself, but I've heard this numerous times.

As an example, check out this blog post: http://thesoftwarerogue./2010/05/porting-of-libcurl-to-android-os-using.html

If the cURL library can be used on Android, I don't see why your networking library couldn't. The difficulties will most likely be related to interacting with the GUI since you don't have access to the platform SDKs from your C/C++ code base. But for a networking lib, that's perfectly OK. Here is what I suggest:

  1. Write some proof of concept code to see how you can link with the lib from both platforms. Consider the limitations in the design of your library.
  2. Develop your networking library as a standalone project with a test driven development approach. That is, write unit tests to drive your code to test it as it's being developed.
  3. Write wrapper classes in native code in both Android and iOS projects to isolate the library code.

Also, as was previously mentioned, Apple is ok with developing apps in C/C++ as long as they are done with XCode. They've even relaxed on that requirement last year.

Good luck

answered Jan 29 '11 at 18:59

Considering the differences it's not really possible. It's a pain in the ass to write a Blackberry/Android shared library and they both use "Java".

If you really want to do cross platform, then you will be writing webpages in javascript and then delivering them on web-views on the devices.

You can't really share code across Objective C and Java, it's a impossibility afaik, and you can't run Java on the iPhone and you can't run ObjectiveC on Android. So there is no possible sane way to do this with native code.

Edit: As far as networking goes, don't think you'll find many places where you can access low-level network across platforms in a uniform way.

answered Dec 19 '10 at 23:17
HaMMeReD
1,4931222

    
Just edited my question. I'll be using C/C++ for my Android apps. –  Mark Dec 20 '10 at 0:11
    
While that simplifies things, it still seems like a exercise in insanity. Maintaining two libraries one for android and one for iPhone is highly likely to be less work then trying to combine them, since their api's and coding styles are so different. If the library is meant for public consumption android c++ is a rare thing, it's not nearly as documented or public as the java side of things. –  HaMMeReD Dec 20 '10 at 6:54

I suppose you are talking about native (not web-based) applications. To make GET/POST HTTP requests in Android you should take a look to HTTPClient. This Java library comes with Android and it allows to develop the features you need:

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
    HttpResponse response = client.execute(request);
    ...
} catch(Exception ex) {
    // handle
}

About how to develop a shared networking library for both iOS and Android, it is an interesting question, I can't figure out any solution because the platforms have too much differences (Java vs Objective C the most obvious), but it would be great.

I can say that Android supports C/C++ code via NDK, but I'm not sure if that works in iOS.

answered Dec 19 '10 at 23:10
Guido García
14.4k1469120

    
What if I were developing all my network code in C/C++? iOS and Android support that no? –  Mark Dec 20 '10 at 0:12
    
Yes, I think so but I'm sure you will experience a lot of problems (don't ask me why). Android via NDK and in iOS I am not sure about how to cross compile your C/C++ to iPhone ARM because I've never tried it. –  Guido García Dec 20 '10 at 7:54

You can try Titanium, source code is in JS then cross compiles to iOS or Android.

answered Dec 20 '10 at 1:38
aromero
12.5k52449

As others have said, it won't be easy, if it's possible at all. If you decide to make an attempt, I would recommend working in C/C++ since both platforms can handle that language. (iOS is Objective-C, as you know, and Android can handle C via the NDK.) Don't bother trying to get Java to run on iOS...

answered Dec 19 '10 at 23:57
Moshe
24.6k37171317

    
Just edited my question. I'll be using C/C++ for my Android apps. –  Mark Dec 20 '10 at 0:11
    
Don't think it would work regardless, you know how Apple is with API access and whatnot. Good luck though! –  Moshe Dec 20 '10 at 0:59
    
@Moshe - Apple is totally fine with C, C++, and Objective-C compiled code in iOS apps. After all, Obj-C is just a superset of C. –  Mark Dec 20 '10 at 1:14
    
@mark - true, but they may have issues with accessing the networking hardware. If you do get something going, please let me know, I'd love to be able to send information from an iPad based kiosk app to local Android phones... –  Moshe Dec 20 '10 at 1:37
    
@Moshe - Fair point. I'll hit you with any good info I find! –  Mark Dec 20 '10 at 1:43

If you can stand the horrid excuse of a language, use Javascript.

answered Dec 19 '10 at 23:17
Lie Ryan
27.5k64690

    
vb.net called... –  mxmissile Dec 20 '10 at 21:20

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多