Android Open Source - android.app.niuz.io Companies Service






From Project

Back to project page android.app.niuz.io.

License

The source code is released under:

GNU General Public License

If you think the Android project android.app.niuz.io listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.niuz.services;
/*  w  w  w.ja  v a  2  s  . c  om*/
import io.niuz.model.CompanyInformation;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

public class CompaniesService extends BaseService {
  private static Map<Integer, String> relevantCompanies = new HashMap<Integer, String>();
  
  private final static String SERVER_LINK_GET_RELEVANT_COMPANIES = SERVER_URL + "getRelevantCompaniesForUsersIntro/";
  private final static String SERVER_LINK_INIT_RELEVANT_COMPANIES = SERVER_URL + "initFavouriteUsersCompanies";
  
  public static void initRelevantCompaniesSelections(CompanyInformation[] objects, Context context) throws Exception {
    // Initializing array
    JSONArray companies = new JSONArray();
    for (CompanyInformation object : objects) {
      if (object.isMarkedAsFavourite()) {
        JSONObject company = new JSONObject();
        company.put("nc", object.getId());
        companies.put(company);
      }
    }
    
    // Forming POST data
    JSONObject objectToSend = new JSONObject();
    
    PhoneService.initPhoneNumber(context.getApplicationContext());
    objectToSend.put("phoneNumber", PhoneService.getPhoneNumber());
    objectToSend.put("companies", companies);
    
    // Posting request
    HttpPost httpPost = new HttpPost(SERVER_LINK_INIT_RELEVANT_COMPANIES);
    httpPost.setEntity(new StringEntity(objectToSend.toString()));
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(httpPost);
    
    // Parsing response, throwing exception if required
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK){
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          response.getEntity().writeTo(out);
          out.close();
          
          if (!new JSONObject(out.toString()).getBoolean("success")) {
            throw new Exception("Something went wrong during API execution!");
          }
          
    } else {
      response.getEntity().getContent().close();
      throw new Exception("Bad http status!");
    }
  }
  
  public static void initRelevantCompaniesForUsersIntro(Context context) throws ClientProtocolException, 
      IOException, JSONException {
    PhoneService.initPhoneNumber(context.getApplicationContext());
    String getUrl = SERVER_LINK_GET_RELEVANT_COMPANIES + PhoneService.getPhoneNumber();
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(new HttpGet(getUrl));
    
    StatusLine statusLine = response.getStatusLine();
    
    if (statusLine.getStatusCode() == HttpStatus.SC_OK){
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          response.getEntity().writeTo(out);
          out.close();
          
          // Initializing companies
          JSONArray companies = new JSONObject(out.toString()).getJSONArray("companies");
          relevantCompanies.clear();
          
          for (int i=0 ; i<companies.length() ; i++) {
            JSONObject company = companies.getJSONObject(i);
            relevantCompanies.put(company.getInt("id"), company.getString("name"));
          }
          
      } else{
          response.getEntity().getContent().close();
          throw new IOException(statusLine.getReasonPhrase());
      }
  }

  public static Map<Integer, String> getRelevantCompanies() {
    return relevantCompanies;
  }

  public static void setRelevantCompanies(Map<Integer, String> relevantCompanies) {
    CompaniesService.relevantCompanies = relevantCompanies;
  }
}




Java Source Code List

io.niuz.CategoriesSelectionActivity.java
io.niuz.CompanyInformationLoadingActivity.java
io.niuz.MainActivity.java
io.niuz.NewsCompaniesSelectionActivity.java
io.niuz.NewsHeadersActivity.java
io.niuz.NewsReaderActivity.java
io.niuz.PreMainActivity.java
io.niuz.model.APIRetrievalStatus.java
io.niuz.model.CategoryInformation.java
io.niuz.model.CompanyInformation.java
io.niuz.model.InitUserStatus.java
io.niuz.model.News.java
io.niuz.services.BaseService.java
io.niuz.services.CategoriesSubcategoriesService.java
io.niuz.services.CompaniesService.java
io.niuz.services.NewsService.java
io.niuz.services.PhoneService.java
io.niuz.services.UserService.java