[英]android application with REST API connection
我正在為我自己的項目開發Android應用程序,還有另一個人在使用API REST控制面板,考慮到我不知道REST和API以及WEB服務,我現在正在學習如何訪問API並發送數據作為JSON和GET數據,但我不確定發生了什么。 例如,我的應用程序中有登錄字段,用戶必須輸入他/她的信息,Android應用程序應將數據發送到控制面板以檢查此信息。 我正在與之合作的人告訴我搜索令牌來訪問網絡服務,但我無法單獨理解這個概念,我需要向我解釋我現在應該做什么以及我應該學會如何正確完成我的應用程序。 最好的祝福
那么你可以使用像okhttp等的HTTP和SPDY客戶端,或者你可以嘗試在Udacity中免費課程。 這是Udacity Android Basics:Networking
1-您必須使用任何庫來學習HTTP連接,我建議Volley為您提供鏈接
2-你必須在這里學習JsonObject和JsonArray
3-這是一個例子:
RequestQueue queue = Volley.newRequestQueue(this);// this = context
final String url = yourURL
// prepare the Request
JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
//Log.d(TAG, "Login Response: " + response.toString());
// displayToast(R.string.toast_email_success);
if (response != null) {
try {
JSONArray JA = response;
int [] id = new int[JA.length()];
String [] name = new String[JA.length()];
String [] premalink = new String[JA.length()];
String [] descreption = new String[JA.length()];
String [] price_html = new String[JA.length()];
String [] stock_status = new String[JA.length()];
int [] stock_quantity = new int[JA.length()];
String [] image_src = new String[JA.length()];
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
JSONArray JA_inside_image = new JSONArray(JO.getJSONArray("images"));
if(JA_inside_image!=null)
{
JSONObject JO_inside = (JSONObject) JA_inside_image.get(0);
image_src[i]=JO_inside.getString("src");
}
if(JO.get("id")!=null) {
id[i] = JO.getInt("id");
}
else
{
id[i]=0;
}
if(JO.get("name")!=null) {
name[i]=JO.getString("name");
if(name[i]=="Product")
break;
}
else
{
name[i]="Not Available Name";
}
if(JO.get("permalink")!=null) {
premalink[i]=JO.getString("permalink");
}
else
{
premalink[i]="Not Available Link";
}
if(JO.get("description")!=null) {
descreption[i]=JO.getString("description");
}
else
{
descreption[i]="Not Available Descreption";
}
if(JO.get("price_html")!=null) {
price_html[i]=JO.getString("price_html");
}
else
{
price_html[i]="Not Available Link";
}
if(JO.get("stock_status")!=null) {
stock_status[i]=JO.getString("stock_status");
}
else
{
stock_status[i]="Not Available Status";
}
if(JO.get("stock_quantity")!=null) {
stock_quantity[i]=JO.getInt("stock_quantity");
}
else
{
stock_quantity[i]=0;
}
}
loadingProgressBar.setVisibility(View.GONE);
} catch (JSONException e)
{
e.printStackTrace();
loadingProgressBar.setVisibility(View.GONE);
}
}
else {
loadingProgressBar.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Sorry Somesthing
Wrong Happend try again later", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
loadingProgressBar.setVisibility(View.GONE);
// Log.d("Error.Response", error.getLocalizedMessage());
}
}
);
您應該閱讀有關HTTPRequests的內容。 您可以使用POST請求將一些數據發送到服務器,也可以使用GET請求從服務器接收數據。 還有一些有用的庫,如Retrofit和Volley,您可以用它們以簡單的方式發送GET,POST,....請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.