2017-01-23 131 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的”, “头”: “测试”, “说明”: “测试”}]} –