Android Open Source - HockeySDK-Android Feedback Parser






From Project

Back to project page HockeySDK-Android.

License

The source code is released under:

Apache License

If you think the Android project HockeySDK-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 net.hockeyapp.android.utils;
//from  w  w w  . j  av a 2  s .c  o m
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.hockeyapp.android.objects.Feedback;
import net.hockeyapp.android.objects.FeedbackAttachment;
import net.hockeyapp.android.objects.FeedbackMessage;
import net.hockeyapp.android.objects.FeedbackResponse;

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

/**
 * <h3>Description</h3>
 * 
 * JSON parser helper class
 * 
 * <h3>License</h3>
 * 
 * <pre>
 * Copyright (c) 2011-2014 Bit Stadium GmbH
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * </pre>
 * 
 * @author Bogdan Nistor
 */
public class FeedbackParser {
  /** Private constructor prevents instantiation from other classes */
  private FeedbackParser() { 
  }

  /**
   * FeedbackParserHolder is loaded on the first execution of FeedbackParser.getInstance() 
   * or the first access to FeedbackParserHolder.INSTANCE, not before.
   */
  private static class FeedbackParserHolder { 
    public static final FeedbackParser INSTANCE = new FeedbackParser();
  }

  public static FeedbackParser getInstance() {
    return FeedbackParserHolder.INSTANCE;
  }
  
  /**
   * Parses JSON string
   *
   * @param feedbackResponseJson string with JSON response
   * @return instance of FeedbackResponse
   */
  public FeedbackResponse parseFeedbackResponse(String feedbackResponseJson) {
    FeedbackResponse feedbackResponse = null;
    Feedback feedback = null;
    if (feedbackResponseJson != null) {
      try {
        JSONObject jsonObject = new JSONObject(feedbackResponseJson);
        
        JSONObject feedbackObject = jsonObject.getJSONObject("feedback");
        feedback = new Feedback();
        
        /** Parse the Messages Array */
        JSONArray messagesArray = feedbackObject.getJSONArray("messages");
        ArrayList<FeedbackMessage> messages = null;
        
        FeedbackMessage feedbackMessage = null;
        if (messagesArray.length() > 0) {
          messages = new ArrayList<FeedbackMessage>();
          
          for (int i = 0; i < messagesArray.length(); i ++) {
            String subject = messagesArray.getJSONObject(i).getString("subject").toString();
            String text = messagesArray.getJSONObject(i).getString("text").toString();
            String oem = messagesArray.getJSONObject(i).getString("oem").toString();
            String model = messagesArray.getJSONObject(i).getString("model").toString();
            String osVersion = messagesArray.getJSONObject(i).getString("os_version").toString();
            String createdAt = messagesArray.getJSONObject(i).getString("created_at").toString();
            int id = messagesArray.getJSONObject(i).getInt("id");
            String token = messagesArray.getJSONObject(i).getString("token").toString();
            int via = messagesArray.getJSONObject(i).getInt("via");
            String userString = messagesArray.getJSONObject(i).getString("user_string").toString();
            String cleanText = messagesArray.getJSONObject(i).getString("clean_text").toString();
            String name = messagesArray.getJSONObject(i).getString("name").toString();
            String appId = messagesArray.getJSONObject(i).getString("app_id").toString();

            JSONArray jsonAttachments = messagesArray.getJSONObject(i).optJSONArray("attachments");
            List<FeedbackAttachment> feedbackAttachments = Collections.emptyList();
            if (jsonAttachments != null) {
              feedbackAttachments = new ArrayList<FeedbackAttachment>();

              for (int j = 0; j < jsonAttachments.length(); j++) {
                int attachmentId = jsonAttachments.getJSONObject(j).getInt("id");
                int attachmentMessageId = jsonAttachments.getJSONObject(j).getInt("feedback_message_id");
                String filename = jsonAttachments.getJSONObject(j).getString("file_name");
                String url = jsonAttachments.getJSONObject(j).getString("url");
                String attachmentCreatedAt = jsonAttachments.getJSONObject(j).getString("created_at");
                String attachmentUpdatedAt = jsonAttachments.getJSONObject(j).getString("updated_at");

                FeedbackAttachment feedbackAttachment = new FeedbackAttachment();
                feedbackAttachment.setId(attachmentId);
                feedbackAttachment.setMessageId(attachmentMessageId);
                feedbackAttachment.setFilename(filename);
                feedbackAttachment.setUrl(url);
                feedbackAttachment.setCreatedAt(attachmentCreatedAt);
                feedbackAttachment.setUpdatedAt(attachmentUpdatedAt);
                feedbackAttachments.add(feedbackAttachment);
              }
            }

            feedbackMessage = new FeedbackMessage();
            feedbackMessage.setAppId(appId);
            feedbackMessage.setCleanText(cleanText);
            feedbackMessage.setCreatedAt(createdAt);
            feedbackMessage.setId(id);
            feedbackMessage.setModel(model);
            feedbackMessage.setName(name);
            feedbackMessage.setOem(oem);
            feedbackMessage.setOsVersion(osVersion);
            feedbackMessage.setSubjec(subject);
            feedbackMessage.setText(text);
            feedbackMessage.setToken(token);
            feedbackMessage.setUserString(userString);
            feedbackMessage.setVia(via);
            feedbackMessage.setFeedbackAttachments(feedbackAttachments);
            messages.add(feedbackMessage);
          }
        }
        
        feedback.setMessages(messages);
        
        try {
          feedback.setName(feedbackObject.getString("name").toString());
        } catch (JSONException e) {
          e.printStackTrace();
        }
    
        try {
          feedback.setEmail(feedbackObject.getString("email").toString());
        } catch (JSONException e) {
          e.printStackTrace();
        }
    
        try {
          feedback.setId(feedbackObject.getInt("id"));
        } catch (JSONException e) {
          e.printStackTrace();
        }
    
        try {
          feedback.setCreatedAt(feedbackObject.getString("created_at").toString());
        } catch (JSONException e) {
          e.printStackTrace();
        }
        
        feedbackResponse = new FeedbackResponse();
        feedbackResponse.setFeedback(feedback);
        try {
          feedbackResponse.setStatus(jsonObject.getString("status").toString());
        } catch (JSONException e) {
          e.printStackTrace();
        }
        
    
        try {
          feedbackResponse.setToken(jsonObject.getString("token").toString());
        } catch (JSONException e) {
          e.printStackTrace();
        }
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }
    
    return feedbackResponse;
  }
}




Java Source Code List

net.hockeyapp.android.Constants.java
net.hockeyapp.android.CrashManagerListener.java
net.hockeyapp.android.CrashManager.java
net.hockeyapp.android.ExceptionHandler.java
net.hockeyapp.android.ExpiryInfoActivity.java
net.hockeyapp.android.FeedbackActivityInterface.java
net.hockeyapp.android.FeedbackActivity.java
net.hockeyapp.android.FeedbackManagerListener.java
net.hockeyapp.android.FeedbackManager.java
net.hockeyapp.android.LocaleManager.java
net.hockeyapp.android.LoginActivity.java
net.hockeyapp.android.LoginManagerListener.java
net.hockeyapp.android.LoginManager.java
net.hockeyapp.android.PaintActivity.java
net.hockeyapp.android.StringListener.java
net.hockeyapp.android.Strings.java
net.hockeyapp.android.Tracking.java
net.hockeyapp.android.UpdateActivityInterface.java
net.hockeyapp.android.UpdateActivity.java
net.hockeyapp.android.UpdateFragment.java
net.hockeyapp.android.UpdateInfoListener.java
net.hockeyapp.android.UpdateManagerListener.java
net.hockeyapp.android.UpdateManager.java
net.hockeyapp.android.adapters.MessagesAdapter.java
net.hockeyapp.android.listeners.DownloadFileListener.java
net.hockeyapp.android.listeners.SendFeedbackListener.java
net.hockeyapp.android.objects.ErrorObject.java
net.hockeyapp.android.objects.FeedbackAttachment.java
net.hockeyapp.android.objects.FeedbackMessage.java
net.hockeyapp.android.objects.FeedbackResponse.java
net.hockeyapp.android.objects.Feedback.java
net.hockeyapp.android.tasks.AttachmentDownloader.java
net.hockeyapp.android.tasks.CheckUpdateTaskWithUI.java
net.hockeyapp.android.tasks.CheckUpdateTask.java
net.hockeyapp.android.tasks.DownloadFileTask.java
net.hockeyapp.android.tasks.GetFileSizeTask.java
net.hockeyapp.android.tasks.LoginTask.java
net.hockeyapp.android.tasks.ParseFeedbackTask.java
net.hockeyapp.android.tasks.SendFeedbackTask.java
net.hockeyapp.android.utils.AsyncTaskUtils.java
net.hockeyapp.android.utils.Base64.java
net.hockeyapp.android.utils.ConnectionManager.java
net.hockeyapp.android.utils.DeviceUtils.java
net.hockeyapp.android.utils.FeedbackParser.java
net.hockeyapp.android.utils.ImageUtils.java
net.hockeyapp.android.utils.PrefsUtil.java
net.hockeyapp.android.utils.SimpleMultipartEntity.java
net.hockeyapp.android.utils.UiThreadUtil.java
net.hockeyapp.android.utils.Util.java
net.hockeyapp.android.utils.VersionCache.java
net.hockeyapp.android.utils.VersionHelper.java
net.hockeyapp.android.utils.ViewHelper.java
net.hockeyapp.android.views.AttachmentListView.java
net.hockeyapp.android.views.AttachmentView.java
net.hockeyapp.android.views.ExpiryInfoView.java
net.hockeyapp.android.views.FeedbackMessageView.java
net.hockeyapp.android.views.FeedbackView.java
net.hockeyapp.android.views.LoginView.java
net.hockeyapp.android.views.PaintView.java
net.hockeyapp.android.views.UpdateView.java