2017-01-23 130 views
-5

我試圖建立與服務器端邏輯 移動應用的移動應用(如服務器發送JSON請求,從網絡上獲取API資源並傳回給移動應用)和非常新這個域名。 這裏有幾點,我很擔心不清楚:與服務器端邏輯

1)如果應用程序是建立在Android的工作室,在那裏做一個從服務器資源的請求通常代碼?

2)我應該使用JSON或REST來自外部的API獲取資源(谷歌等)

3)是否有一個Java框架,我可以點擊與Apache實現服務器端邏輯Tomcat的?

任何意見或指向我正確的方向將是真棒。謝謝!

編輯:如果我的問題是不清楚的,真的很感激,如果你糾正我,而不是downvoting

回答

1

的我想你應該瞭解在android系統聯網。爲此,你必須使用AsyncTask。在AsyncTask的doInBackground方法中,您需要從服務器獲得正確的響應,然後您可以從中創建自定義對象,並使用onPostExecute方法編寫業務邏輯。

+0

啊,這樣的叫法,我將尋找更多的成 – Jackelll

2

我並不很清楚你的查詢是什麼,但我會給出怎樣的數據可以從服務器調用到移動應用程序的一些見解。 您可以使用AsyncTask在後臺獲取數據,而不會影響您的主線程。

調用的AsyncTask在YOUT onCreate方法

new getDataList().execute(); 

如:

public class getDataList extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> { 
     protected void onPreExecute(){ 
     } 
     @Override 
     protected ArrayList<String> doInBackground(ArrayList<String>... passing) { 
      // Locate the WorldhavingDelay Class 
      // JSON file URL address 
      ArrayList<String> result = new ArrayList<String>(); 
      //52.77.68.231 
      ncardsmore.clear(); 
      try { 
       jsonobject = JSONfunctions 
         .getJSONfromURL("http://52.42.251.70/testData/test2.php?language=" + myLanguage); 
       Log.e("JSONARRAY", jsonobject.toString()); 
       // Locate the NodeList name 
       if (jsonobject != null){ 
        jsonarray = jsonobject.getJSONArray("news"); 
       for (int i = 0; i < jsonarray.length(); i++) { 
        if (isCancelled()) 
         break; 
        String myId = Integer.toString(i); 
        jsonobject = jsonarray.getJSONObject(i); 
        exist = jsonobject.optString("exist"); 
        if (exist.equals("true")) { 
         id = jsonobject.optString("id"); 
         imageUrl = jsonobject.optString("imageUrl"); 
         myHeader = jsonobject.optString("header"); 
         description = jsonobject.optString("description"); 
         //mydate = jsonobject.optString("date"); 
         mydate = "12/12/12"; 
         //adding to database 
         mydb.addNews(new NewsData(id, myHeader, description, imageUrl, myId)); 
         result.add("exist"); 
        } else { 
         result.add("notExist"); 
        } 
       } 
      } 
      else{ 
        result.add("error"); 
       } 

      } catch (Exception e) { 
      } 
      return result; 
     } 
     protected void onPostExecute(ArrayList<String> result) { 
      String existornot = result.get(0); 
      //Use your code here 
    } 

創建getJSONfromURL方法單獨的類(即JSONfunctions)

import android.util.Log; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

public class JSONfunctions { 

    public static JSONObject getJSONfromURL(String url) { 
     InputStream is = null; 
     String result = ""; 
     String defaultVal = ""; 
     JSONObject jArray = null; 

     // Download JSON data from URL 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     } catch (Exception e) { 
      jArray = null; 
      Log.e("log_tag", "Error in http connection " + e.toString()); 
      // return err; 
     } 

     // Convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "utf-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
     } catch (Exception e) { 

     } 

     try { 

      jArray = new JSONObject(result); 
     } catch (JSONException e) { 
      jArray = null; 
      Log.e("log_tag", "Error parsing data " + e.toString()); 
     } 

     return jArray; 
    } 
} 

我希望你得到它。

+0

這有助於澄清客戶端 - 服務器之間的通信術語。這是否意味着在服務器端(apache Tomcat),我必須在servlet上實現JSON代碼以從外部API請求資源? – Jackelll

+1

@Jackelll是的..你所要做的就是以json字符串的形式提供響應。例如:{「news」:[{「exist」:「true」,「id」:「2」,「imageUrl」:「http://www.planwallpaper.com/static/images/nasas-images-of-最顯着的事件,你-着-Miss.jpg的」, 「頭」: 「測試」, 「說明」: 「測試」}]} –