Android Open Source - visiting-card-android Parser






From Project

Back to project page visiting-card-android.

License

The source code is released under:

GNU General Public License

If you think the Android project visiting-card-android 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 com.matrix.visitingcard.http.parser;
/*w  ww. j  a va  2 s .c  o m*/
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.matrix.visitingcard.http.response.VC;
import com.matrix.visitingcard.http.response.VC.ImageUrl;
import com.matrix.visitingcard.http.response.VC.KeysAndValues;
import com.matrix.visitingcard.http.response.VCTResponse;
import com.matrix.visitingcard.http.response.VCTResponse.KeysAndTypes;
import com.matrix.visitingcard.http.response.VCTResponse.KeysType;
import com.matrix.visitingcard.logger.VLogger;
import com.matrix.visitingcard.user.User;

public class Parser {

  /**
   * { "is_new_user": false, "user": { "id": 4, "name": "Michel Vin",
   * "created_at": "2014-06-03T18:29:19.354Z", "updated_at":
   * "2014-06-04T16:30:20.772Z", "email": "testing98459845@gmail.com" } }
   * 
   * @param content
   * @return
   */
  public static void parseSocialLogin(byte[] content) {

    if (content == null || content.length == 0) {
      VLogger.e("content is null");
      return;
    }

    User user = User.getInstance();

    try {
      JSONObject jsonObject = new JSONObject(new String(content));

      user.setNewUser(jsonObject.getBoolean("is_new_user"));

      jsonObject = jsonObject.getJSONObject("user");// reusing same
                              // variable :P
      user.setId(jsonObject.getString("id"));
      user.setName(jsonObject.getString("name"));
      user.setCreatedAt(jsonObject.getString("created_at"));
      user.setUpdatedAt(jsonObject.getString("updated_at"));
      user.setEmail(jsonObject.getString("email"));

    } catch (JSONException e) {
      e.printStackTrace();
    }

  }

  public static ArrayList<VCTResponse> parseVCT(byte[] content) {

    if (content == null || content.length == 0) {
      VLogger.e("content is null");
      return null;
    }

    ArrayList<VCTResponse> vcts = new ArrayList<VCTResponse>();

    try {
      JSONArray jsonArray = new JSONArray(new String(content));

      for (int i = 0; i < jsonArray.length(); i++) {
        VCTResponse vct = new VCTResponse();
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        vct.setId(jsonObject.getInt("id"));
        vct.setName(jsonObject.getString("name"));

        JSONObject jsonSampleUrls = jsonObject
            .getJSONObject("sample_urls");
        VCTResponse.SampleUrls sampleUrls = vct.new SampleUrls();

        sampleUrls.setOriginal(jsonSampleUrls.getString("original"));
        sampleUrls.setThumb(jsonSampleUrls.getString("thumb"));
        sampleUrls.setMedium(jsonSampleUrls.getString("medium"));

        vct.setSampleUrls(sampleUrls);

        JSONArray jsonKeysAndTypes = jsonObject
            .getJSONArray("keys_and_types");
        ArrayList<VCTResponse.KeysAndTypes> keysAndTypes = new ArrayList<VCTResponse.KeysAndTypes>();

        for (int j = 0; j < jsonKeysAndTypes.length(); j++) {
          JSONObject jsonKeyAndType = jsonKeysAndTypes
              .getJSONObject(j);
          String typeString = jsonKeyAndType.getString("type");
          String key = jsonKeyAndType.getString("key");

          KeysAndTypes keyAndType = vct.new KeysAndTypes();

          KeysType type = KeysType.TEXT;

          if (typeString.equalsIgnoreCase(KeysType.TEXTAREA.name())) {
            type = KeysType.TEXTAREA;
          } else if (typeString.equalsIgnoreCase(KeysType.IMAGE
              .name())) {
            type = KeysType.IMAGE;
          }
          keyAndType.setType(type);
          keyAndType.setKey(key);
          keysAndTypes.add(keyAndType);

        }

        vct.setKeysAndTypes(keysAndTypes);

        vcts.add(vct);
      }

    } catch (JSONException e) {
      e.printStackTrace();
    }
    return vcts;
  }

  public static ArrayList<VC> parseVC(byte[] content) {

    if (content == null || content.length == 0) {
      VLogger.e("content is null");
      return null;
    }

    ArrayList<VC> vcs = new ArrayList<VC>();

    try {
      JSONArray jsonArray = new JSONArray(new String(content));

      for (int i = 0; i < jsonArray.length(); i++) {
        VC vc = new VC();
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        vc.setId(jsonObject.getInt("id"));
        vc.setUserId(jsonObject.getInt("user_id"));
        vc.setVisitingCardTemplateId(jsonObject
            .getInt("visiting_card_template_id"));
        vc.setCreatedAt(jsonObject.getString("created_at"));
        vc.setUpdatedAt(jsonObject.getString("updated_at"));

        JSONObject jsonSampleUrls = jsonObject
            .getJSONObject("image_url");
        ImageUrl imageUrl = vc.new ImageUrl();

        imageUrl.setOriginal(jsonSampleUrls.getString("original"));
        imageUrl.setThumb(jsonSampleUrls.getString("thumb"));
        imageUrl.setMedium(jsonSampleUrls.getString("medium"));

        vc.setImagUrls(imageUrl);

        JSONArray jsonKeysAndValues = jsonObject
            .getJSONArray("visiting_card_datas");
        ArrayList<KeysAndValues> keysAndValues = new ArrayList<KeysAndValues>();

        for (int j = 0; j < jsonKeysAndValues.length(); j++) {
          JSONObject jsonKeyAndValue = jsonKeysAndValues
              .getJSONObject(j);
          String key = jsonKeyAndValue.getString("key");
          String value = jsonKeyAndValue.getString("value");

          KeysAndValues keyAndValue = vc.new KeysAndValues();

          keyAndValue.setKey(key);
          keyAndValue.setValue(value);
          keysAndValues.add(keyAndValue);

        }

        vc.setKeysAndValues(keysAndValues);

        vcs.add(vc);
      }

    } catch (JSONException e) {
      e.printStackTrace();
    }
    return vcs;
  }

  public static User parseUser(byte[] content) {
    if (content == null || content.length == 0) {
      VLogger.e("content is null");
      return null;
    }

    User user = User.getInstance();

    try {
      JSONObject jsonObject = new JSONObject(new String(content));

      user.setNewUser(false);
      user.setId(jsonObject.getString("id"));
      user.setName(jsonObject.getString("name"));
      user.setCreatedAt(jsonObject.getString("created_at"));
      user.setUpdatedAt(jsonObject.getString("updated_at"));
      user.setEmail(jsonObject.getString("email"));

    } catch (JSONException e) {
      e.printStackTrace();
    }

    return user;
  }

}




Java Source Code List

android.UnusedStub.java
com.loopj.android.http.AsyncHttpClient.java
com.loopj.android.http.AsyncHttpRequest.java
com.loopj.android.http.AsyncHttpResponseHandler.java
com.loopj.android.http.Base64DataException.java
com.loopj.android.http.Base64OutputStream.java
com.loopj.android.http.Base64.java
com.loopj.android.http.BaseJsonHttpResponseHandler.java
com.loopj.android.http.BinaryHttpResponseHandler.java
com.loopj.android.http.DataAsyncHttpResponseHandler.java
com.loopj.android.http.FileAsyncHttpResponseHandler.java
com.loopj.android.http.JsonHttpResponseHandler.java
com.loopj.android.http.JsonStreamerEntity.java
com.loopj.android.http.MyRedirectHandler.java
com.loopj.android.http.MySSLSocketFactory.java
com.loopj.android.http.PersistentCookieStore.java
com.loopj.android.http.PreemtiveAuthorizationHttpRequestInterceptor.java
com.loopj.android.http.RangeFileAsyncHttpResponseHandler.java
com.loopj.android.http.RequestHandle.java
com.loopj.android.http.RequestParams.java
com.loopj.android.http.ResponseHandlerInterface.java
com.loopj.android.http.RetryHandler.java
com.loopj.android.http.SaxAsyncHttpResponseHandler.java
com.loopj.android.http.SerializableCookie.java
com.loopj.android.http.SimpleMultipartEntity.java
com.loopj.android.http.SyncHttpClient.java
com.loopj.android.http.TextHttpResponseHandler.java
com.loopj.android.http.package-info.java
com.matrix.asynchttplibrary.AsyncH.java
com.matrix.asynchttplibrary.annotation.AsyncHAnnotation.java
com.matrix.asynchttplibrary.annotation.AsyncHIgnoreParam.java
com.matrix.asynchttplibrary.logger.ALogger.java
com.matrix.asynchttplibrary.model.CallProperties.java
com.matrix.asynchttplibrary.parser.AsyncParser.java
com.matrix.asynchttplibrary.request.AsyncRequestHeader.java
com.matrix.asynchttplibrary.request.AsyncRequestParam.java
com.matrix.asynchttplibrary.response.AsyncResponseBody.java
com.matrix.asynchttplibrary.security.CustomSSLSocketFactory.java
com.matrix.asynchttplibrary.util.AsyncUtil.java
com.matrix.visitingcard.AllVCFragment.java
com.matrix.visitingcard.CreateVCActivity.java
com.matrix.visitingcard.ListMyVCFragment.java
com.matrix.visitingcard.ListMyVCRActivity.java
com.matrix.visitingcard.ListOfVCTFragment.java
com.matrix.visitingcard.ResideActivity.java
com.matrix.visitingcard.SelectVCActivity.java
com.matrix.visitingcard.ShareVCDialogFragment.java
com.matrix.visitingcard.SignUpFormActivity.java
com.matrix.visitingcard.SplashScreenActivity.java
com.matrix.visitingcard.VCRCreateDialogFragment.java
com.matrix.visitingcard.ViewVC.java
com.matrix.visitingcard.adapter.SupportArrayAdapter.java
com.matrix.visitingcard.adapter.VCAdapter.java
com.matrix.visitingcard.adapter.VCRAdapter.java
com.matrix.visitingcard.adapter.VCTAdapter.java
com.matrix.visitingcard.constant.Constants.java
com.matrix.visitingcard.gcm.GcmBroadcastReceiver.java
com.matrix.visitingcard.gcm.GcmIntentService.java
com.matrix.visitingcard.http.AsyncHttp.java
com.matrix.visitingcard.http.ProgressJSONResponseCallBack.java
com.matrix.visitingcard.http.ProgressJsonHttpResponseHandler.java
com.matrix.visitingcard.http.UIReloadCallBack.java
com.matrix.visitingcard.http.parser.Parser.java
com.matrix.visitingcard.http.request.AcceptVCRResquest.java
com.matrix.visitingcard.http.request.ShareVCResquest.java
com.matrix.visitingcard.http.request.SocialLoginRequest.java
com.matrix.visitingcard.http.response.FriendsVC.java
com.matrix.visitingcard.http.response.MyVC.java
com.matrix.visitingcard.http.response.VCR.java
com.matrix.visitingcard.http.response.VCTResponse.java
com.matrix.visitingcard.http.response.VC.java
com.matrix.visitingcard.logger.VLogger.java
com.matrix.visitingcard.user.User.java
com.matrix.visitingcard.util.CustomImageDownaloder.java
com.matrix.visitingcard.util.FileUtil.java
com.matrix.visitingcard.util.SharedPrefs.java
com.matrix.visitingcard.util.Util.java
com.matrix.visitingcard.util.VisitingCardApp.java
com.special.ResideMenu.ResideMenuItem.java
com.special.ResideMenu.ResideMenu.java
com.special.ResideMenu.TouchDisableView.java