问题描述
我是新来的机器人。我其实是一个实习生在一个创业公司工作,所以我通过自己学习的东西。我的团队领导人刚刚问我写一个应用程序从Android应用程序调用REST API。他让我从阅读下面的图片(底部)的状态值并显示它的价值在一个新的活动。其实,我不知道我应该做的。到目前为止,我只学会基本的东西之类的活动之间传递值。请建议我一个很好的教程,或者给我一个想法开始。
I'm new to android. I'm actually a trainee working in a startup company, so I've to learn things by myself. My team leader has just asked me to write an app to call a REST API from an android app. He asked me to read the status value from the picture below(at the bottom) and show its value in a new activity. I actually have no idea what I'm supposed to do. So far I've learned only basic things like passing values between activities. Please suggest me a good tutorial, or give me an idea to start with.
推荐答案
请使用异步HTTP库。
Please use asynch http library.
下面的链接将解释每一件事情一步一步来。
below link will explain every thing step by step.
http://loopj.com/android-async-http/
下面是示例应用程序:
-
http://www.techrepublic.com/blog/software-engineer/calling-restful-services-from-your-android-app/
http://blog.strikeiron.com/bid/73189/Integrate-a-REST-API-into-Android-Application-in-less-than-15-minutes
创建一个类:
public class HttpUtils {
private static final String BASE_URL = "http://api.twitter.com/1/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(getAbsoluteUrl(url), params, responseHandler);
}
public static void getByUrl(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.get(url, params, responseHandler);
}
public static void postByUrl(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client.post(url, params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
调用方法:
RequestParams rp = new RequestParams();
rp.add("username", "aaa"); rp.add("password", "aaa@123");
HttpUtils.post(AppConstant.URL_FEED, rp, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
// If the response is JSONObject instead of expected JSONArray
Log.d("asd", "---------------- this is response : " + response);
try {
JSONObject serverResp = new JSONObject(response.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
// Pull out the first event on the public timeline
}
});
}
请在你的manifest文件给网络的权限。
Please give internet permission in your manifest file.
<uses-permission android:name="android.permission.INTERNET" />
这篇关于从一个Android应用程序调用REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!