【问题标题】:Make a REST API call from an IntentService or an AsyncTask?从 IntentService 或 AsyncTask 调用 REST API?
【发布时间】:2011-11-01 08:26:01
【问题描述】:

想象一个典型的场景,一个活动打开,需要调用一个 REST HTTP API 来获取一些内容,并且一旦收到更新 UI。显然 API 调用需要在单独的线程上进行,但应该使用 AsyncTask、IntentService 还是其他方法来完成,为什么?

【问题讨论】:

    标签: android android-asynctask intentservice


    【解决方案1】:

    我会推荐IntentServiceResultReceiver 的组合,如this post 中所述。

    还可以查看实现此模型的Google iosched。 2010 版展示了如何使用 onRetainNonConfigurationInstancegetLastNonConfigurationInstance 和静态类来持久化 ResultReceiver 的配置更改(即屏幕旋转)。

    我已经在一个应用程序中成功地实现了这个模型。如果您对所提供的链接有任何问题,请告诉我。

    编辑:我忘记了“为什么”的问题;)

    AsyncTask 与活动紧密绑定,它不能很好地处理配置更改。对于活动生命周期内的短期任务(onResumeonPause 之间),这很好。我将HttpClient 用于我的REST API 调用,连接超时和套接字超时设置为20 秒(由于移动网络不佳......)。这意味着 API 调用可以持续长达 40 秒。你肯定想在服务中处理这么长的任务,而不是AsyncTask

    【讨论】:

    • 我有一个问题要问@hleroy,那么我什么时候应该启动 IntentService 或者 OnResume()、OnStart() 或 OnClickEventListener(),因为我需要使用 SharedPreference 在本地保存以供离线使用。例如,我需要使用 Webservice(在我的情况下为 WCF web api)从服务器的 UI 中列出 200 个学生,那么我应该将这行代码放在哪里`//启动意图服务//运行意图服务//意图下载意图 = 新意图(this.Context, typeof(StudentIntentService)); //this.Context.StartService(downloadIntent);`
    【解决方案2】:

    回收站视图

      <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    第二活动

    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.provider.SyncStateContract;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.widget.HorizontalScrollView;
    import android.widget.Toast;
    
    import com.bumptech.glide.util.Util;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    
    public class SecondActivity extends AppCompatActivity
    {
        public RecyclerView recyclerView;
        public Utils utils;
        public Setget setget;
        public ArrayList<Setget>  arrayList;
        public StudentShowAdatpter studentShowAdatpter;
        public  static final String BASE_URL="http://api/";
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            recyclerView= (RecyclerView) findViewById(R.id.recycler_view);
    
            utils=new Utils(SecondActivity.this);
            if(utils.isConnectingToInternet(SecondActivity.this))
            {
                new getStudentData().execute();
            }
            else
            {
                Toast.makeText(SecondActivity.this, "Please check internter connection.s", Toast.LENGTH_SHORT).show();
            }
        }
    
        private class getStudentData extends AsyncTask<String,String,String>
        {
            public ProgressDialog pd;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pd=new ProgressDialog(SecondActivity.this);
                pd.setMessage("Loading...");
                pd.setCancelable(false);
                pd.show();
            }
    
            @Override
            protected String doInBackground(String... params)
            {
                String str_response=utils.getResponseofGet(BASE_URL+"get.php?id=7");
                return str_response;
            }
    
            @Override
            protected void onPostExecute(String s)
            {
                super.onPostExecute(s);
                pd.dismiss();
                try {
                    JSONObject jsonObject=new JSONObject(s);
                    if(jsonObject.getString("status").equalsIgnoreCase("true"))
                    {
                        JSONArray jsonArray=jsonObject.getJSONArray("detail");
                            for (int i=0;i<jsonArray.length();i++)
                            {
                                JSONObject object=jsonArray.getJSONObject(i);
                                setget=new Setget();
                                setget.setStud_name(object.getString(""));
                                setget.setStud_date(object.getString(""));
                                arrayList.add(setget);
                            }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
    
                if(arrayList!=null)
                {
                    studentShowAdatpter = new StudentShowAdatpter(SecondActivity.this, arrayList);
                    recyclerView.setLayoutManager(new LinearLayoutManager(SecondActivity.this));
                    //recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(studentShowAdatpter);
                }
                else
                {
                    Toast.makeText(SecondActivity.this, "Data Not Found", Toast.LENGTH_SHORT).show();
                }
            }
        }
    
    }
    

    适配器

    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    
    public class StudentShowAdatpter extends RecyclerView.Adapter<StudentShowAdatpter.MyViewHolder>
    {
    public Setget setget;
    public ArrayList<Setget> arrayList;
    Context context;
    public StudentShowAdatpter(SecondActivity secondActivity, ArrayList<Setget> arrayList) {
        this.arrayList=arrayList;
        this.context=secondActivity;
    }
    
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_stud_list, parent, false);
        return new MyViewHolder(itemView);
    }
    
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
            setget=arrayList.get(position);
            holder.stud_name.setText(setget.getStud_name());
            holder.stud_date.setText(setget.getStud_date());
    }
    
       @Override
    public int getItemCount() {
        return arrayList.size();
    }
    
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView stud_name, stud_date;
    
        public MyViewHolder(View view) {
            super(view);
            stud_date = (TextView) view.findViewById(R.id.stud_date);
            stud_name = (TextView) view.findViewById(R.id.stud_name);
        }
    }
    }
    

    【讨论】:

      【解决方案3】:

      使用 AsyncTask 调用 API 并在 RECYCLERVIEW 中显示登录和数据

      build.gradle(Module.app)

      dependencies {
           compile 'com.android.support:recyclerview-v7:23.3.+'
           compile 'com.android.support:cardview-v7:23.3.+'
           compile 'com.github.bumptech.glide:glide:3.7.0'
      }
      

      AndroidManifest.xml

      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
      <uses-permission android:name="android.permission.INTERNET"/>
      

      login.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginLeft="20dp"
          android:layout_marginTop="15dp"
          android:layout_marginRight="10dp"
          android:orientation="vertical">
      
          <EditText
              android:id="@+id/edit_email"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="Enter Email" />
      
          <EditText
              android:id="@+id/edit_password"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="Enter Password" />
      
          <Button
              android:id="@+id/btn_login"
              android:layout_width="match_parent"
              android:layout_height="40dp"
              android:text="Login" />
      
      </LinearLayout>
      

      MainActivity.java

      import android.app.ProgressDialog;
      import android.content.Intent;
      import android.os.AsyncTask;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.Toast;
      
      import org.json.JSONException;
      import org.json.JSONObject;
      
      import java.util.HashMap;
      
      public class MainActivity extends AppCompatActivity
      {
      public EditText edit_email,edit_password;
      public Button btn_login;
      public String str_email,str_password;
      public Utils utils;
      public String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+.[a-z]+";
      public  static final String BASE_URL="ffyy/";
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          utils=new Utils(MainActivity.this);
          edit_email= (EditText) findViewById(R.id.edit_email);
          edit_password= (EditText) findViewById(R.id.edit_password);
          btn_login= (Button) findViewById(R.id.btn_login);
      
          btn_login.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v)
              {
                  if (edit_email.getText().toString().trim().length() == 0)
                  {
                      edit_email.setError("Enter Email Id");
                      return;
                  }
                  else if (edit_password.getText().toString().trim().length() == 0) {
                      edit_password.setError("Enter Password");
                      return;
                  }
                  else {
                      str_email = edit_email.getText().toString();
                      str_password = edit_password.getText().toString();
                      if (utils.isConnectingToInternet(MainActivity.this)) {
                          new getLoginData().execute();
                      } else {
                          Toast.makeText(MainActivity.this, "Please check internet connection.", Toast.LENGTH_SHORT).show();
                          }
                  }
              }
          });
      }
      
      private class getLoginData extends AsyncTask<String,String,String>
      {
          public ProgressDialog pd;
      
          @Override
          protected void onPreExecute()
          {
              super.onPreExecute();
              pd=new ProgressDialog(MainActivity.this);
              pd.setMessage("Loading...");
              pd.setCancelable(false);
              pd.show();
          }
      
          @Override
          protected String doInBackground(String... params)
          {
              HashMap<String,String> hashMap=new HashMap<>();
              hashMap.put("email", str_email);
              hashMap.put("password", str_password);
              String str_response= utils.getResponseofPost(BASE_URL+"login_for_all.php",hashMap);
              return str_response;
          }
      
          @Override
          protected void onPostExecute(String s)
          {
              super.onPostExecute(s);
              pd.dismiss();
              try {
                  JSONObject jsonObject=new JSONObject(s);
                  if(jsonObject.getString("status").equalsIgnoreCase("true"))
                  {
                      Toast.makeText(MainActivity.this, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
      
                  }
                  else
                  {
                      Toast.makeText(MainActivity.this, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
      }
      }
      

      Utils.java

         import android.content.Context;
      import android.content.SharedPreferences;
      import android.content.pm.PackageManager;
      import android.graphics.Bitmap;
      import android.location.Location;
      import android.net.ConnectivityManager;
      import android.net.Network;
      import android.net.NetworkInfo;
      import android.os.Build;
      import android.os.StrictMode;
      import android.preference.PreferenceManager;
      import android.util.Base64;
      import android.util.Log;
      
      
      import java.io.BufferedReader;
      import java.io.BufferedWriter;
      import java.io.ByteArrayOutputStream;
      import java.io.InputStreamReader;
      import java.io.OutputStream;
      import java.io.OutputStreamWriter;
      import java.io.UnsupportedEncodingException;
      import java.lang.reflect.Type;
      import java.net.HttpURLConnection;
      import java.net.URL;
      import java.net.URLEncoder;
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.HashMap;
      import java.util.Map;
      
      import javax.net.ssl.HttpsURLConnection;
      
      
      public class Utils {
          static Context context;
          SharedPreferences sp;
          Location getLocation;
      
          public Utils(Context context) {
              this.context = context;
              //sp = context.getSharedPreferences(Constant.Prefrence, Context.MODE_PRIVATE);
          }
      
         /* public String getReadSharedPrefrenceIsFirstTime(Context activity, String userid) {
              String defValue = "";
              String resposne = sp.getString(Constant.ISFIRSTTIME, defValue);
              return resposne;
          }
      */
      /*
          public static String convertToGson(Object object) {
              Gson gson = new Gson();
              String loginResult = gson.toJson(object);
              return loginResult;
          }
      
          public static <T> T GsonToObject(String strObj, Type classObject) {
              Gson gson = new Gson();
              return gson.fromJson(strObj, classObject);
          }
      */
      
      
          public void clearSharedPreferenceData() {
              sp.edit().clear().commit();
          }
      
          public String getResponseofPost(String URL, HashMap<String, String> postDataParams) {
              URL url;
              String response = "";
              try {
                  url = new URL(URL);
                  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  conn.setReadTimeout(15000);
                  conn.setConnectTimeout(15000);
                  conn.setRequestMethod("POST");
                  conn.setDoInput(true);
                  conn.setDoOutput(true);
                  OutputStream os = conn.getOutputStream();
                  BufferedWriter writer = new BufferedWriter(
                          new OutputStreamWriter(os, "UTF-8"));
      
                  writer.write(getPostDataString(postDataParams));
                  writer.flush();
                  writer.close();
                  os.close();
                  int responseCode = conn.getResponseCode();
                  if (responseCode == HttpsURLConnection.HTTP_OK) {
                      String line;
                      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      while ((line = br.readLine()) != null) {
                          response += line;
                      }
                  } else {
                      response = "";
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return response;
          }
      
          public static String getResponseofGet(String URL) {
              URL url;
              String response = "";
              try {
      
                  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                  StrictMode.setThreadPolicy(policy);
                  url = new URL(URL);
                  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  conn.setReadTimeout(15000);
                  conn.setConnectTimeout(15000);
                  conn.setRequestMethod("GET");
                  conn.setDoInput(true);
                  conn.setDoOutput(true);
      
                  OutputStream os = conn.getOutputStream();
                  BufferedWriter writer = new BufferedWriter(
                          new OutputStreamWriter(os, "UTF-8"));
      
                  writer.flush();
                  writer.close();
                  os.close();
                  int responseCode = conn.getResponseCode();
                  Log.d("URL - ResponseCode", URL + " - " + responseCode);
                  if (responseCode == HttpsURLConnection.HTTP_OK) {
                      String line;
                      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      while ((line = br.readLine()) != null) {
                          response += line;
                      }
                  } else {
                      response = "";
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return response;
          }
      
          private String getPostDataString(HashMap<String, String> params) throws
                  UnsupportedEncodingException {
              StringBuilder result = new StringBuilder();
              boolean first = true;
              for (Map.Entry<String, String> entry : params.entrySet()) {
                  if (first)
                      first = false;
                  else
                      result.append("&");
      
                  result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                  result.append("=");
                  result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
              }
              return result.toString();
          }
      
          public String MakeServiceCall(String URLSTR) {
              StringBuilder response = null;
              try {
                  response = new StringBuilder();
                  URL url = new URL(URLSTR);
                  HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
                  if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                      BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()), 8192);
                      String strLine = null;
                      while ((strLine = input.readLine()) != null) {
                          response.append(strLine);
                      }
                      input.close();
                  }
              } catch (Exception e) {
              }
              return response.toString();
          }
      
          private boolean appInstalledOrNot(String packageName) {
              PackageManager pm = context.getPackageManager();
              boolean app_installed;
              try {
                  pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                  app_installed = true;
              } catch (PackageManager.NameNotFoundException e) {
                  app_installed = false;
              }
              return app_installed;
          }
      
          public static boolean isConnectingToInternet(Context context)
          {
              ConnectivityManager connectivityManager = (ConnectivityManager) context
                      .getSystemService(Context.CONNECTIVITY_SERVICE);
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                  Network[] networks = connectivityManager.getAllNetworks();
                  NetworkInfo networkInfo;
                  for (Network mNetwork : networks)
                  {
                      networkInfo = connectivityManager.getNetworkInfo(mNetwork);
                      if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                          return true;
                      }
                  }
              } else {
                  if (connectivityManager != null)
                  {
                      NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
                      if (info != null) {
                          for (NetworkInfo anInfo : info) {
                              if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                                  return true;
                              }
                          }
                      }
                  }
              }
              return false;
          }
      
          public static void WriteSharePrefrence(Context context, String key, String value)
          {
              final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
              final SharedPreferences.Editor editor = preferences.edit();
              editor.putString(key, value);
              editor.commit();
          }
      
          public static String ReadSharePrefrence(Context context, String key)
          {
              String data;
              final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
              final SharedPreferences.Editor editor = preferences.edit();
              data = preferences.getString(key, "");
              editor.commit();
              return data;
          }
      
      
      
      
          public static void Clear(Context context, String key)
          {
              String data;
              final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
              final SharedPreferences.Editor editor = preferences.edit().clear();
              editor.commit();
          }
      
          public static void ClearaSharePrefrence(Context context) {
              String data;
              final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
              final SharedPreferences.Editor editor = preferences.edit().clear();
              editor.commit();
          }
      
          public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
              ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
              image.compress(compressFormat, quality, byteArrayOS);
              return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
          }
      
          public static String getLeaveStatus(String status) {
              String result = "";
              switch (status) {
                  case "0":
                      result = "Pending";
                      break;
                  case "1":
                      result = "Aproved";
                      break;
                  case "2":
                      result = "Cancel";
                      break;
              }
              return result;
          }
      
      
          public String parseDateToddMMyyyy(String time)
          {
              String inputPattern = "yyyy-MM-dd HH:mm:ss";
              String outputPattern = "yyyy-MM-dd";
              SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
              SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
              Date date = null;
              String str = null;
              try {
                  date = inputFormat.parse(time);
                  str = outputFormat.format(date);
              } catch (ParseException e) {
                  e.printStackTrace();
              }
              return str;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-19
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 2015-09-28
        • 2015-06-01
        相关资源
        最近更新 更多