Android Open Source - ImproveMyCity-Mobile Upload_ Data






From Project

Back to project page ImproveMyCity-Mobile.

License

The source code is released under:

GNU General Public License

If you think the Android project ImproveMyCity-Mobile 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

// Upload_Data 
package com.mk4droid.IMC_Services;
/* w w w.ja  v a  2s . co  m*/
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.util.Log;

import com.mk4droid.IMC_Store.Constants_API;
import com.mk4droid.IMC_Store.Phptasks;
import com.mk4droid.IMC_Utils.RestCaller;
/**
 * Major methods to upload data to remote server are gathered in this class.
 *
 * @copyright   Copyright (C) 2012 - 2013 Information Technology Institute ITI-CERTH. All rights reserved.
 * @license     GNU Affero General Public License version 3 or later; see LICENSE.txt
 * @author      Dimitrios Ververidis for the Multimedia Group (http://mklab.iti.gr). 
 *
 */
public class Upload_Data {
  
  // ------------------------ SendVote ------------------
  /**
   * Sent vote for IssueID by UserID
   * 
   * @param UserID
   * @param IssueID
   * @param UserNameSTR
   * @param PasswordSTR
   * @return
   */
    public static boolean SendVote(int UserID, int IssueID, String UserNameSTR, String PasswordSTR){
      
      RestCaller rc = new RestCaller();  
    String response =  rc.now(
          Constants_API.COM_Protocol + Constants_API.ServerSTR + Constants_API.phpExec , "GET", 
                           new String[]{
                                    "option", "com_improvemycity", 
                                    "task",    Phptasks.TASK_VOTE,
                                    "format", "json",
                                    "issueId", Integer.toString(IssueID),
                                    "userid",  Integer.toString(UserID),
                                    "username", UserNameSTR,
                                    "password", Security.EncWrapper(PasswordSTR)}, "UTF-8", "SendVote");
      if (response!=null)
        return true;
      else 
        return false;
        
  }
  
    //================ Register ==================
    /**
     * Register user
     *  
     * @param username    ASCII chars
     * @param email       
     * @param password    up to 16 ASCII chars
     * @param name        UTF-8 chars
     * @return
     */
    public static String SendRegistrStreaming(String username, String email, String password, String name){
      
      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;

      String lineEnd    = "\r\n";
      String twoHyphens = "--";
      String boundary   =  "*****";

      try
      {
        String urlSTR = Constants_API.COM_Protocol_S + 
                    Constants_API.ServerSTR + 
                    Constants_API.remoteImages + 
                    Phptasks.TASK_REGISTER_USER;
        
        URL url    = new URL( urlSTR  );
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",  "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream(connection.getOutputStream() );
                    
        //---------------- Params ------------
        // 1 Title
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"username\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(username + lineEnd); 
        
        // 2 userid
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"email\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(email + lineEnd);
             
        // 3 passwordSTR 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"password\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        String passEnc = Security.EncWrapper(password);
        outputStream.writeBytes( passEnc + lineEnd);
        
        // 4 name
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"name\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write((name + lineEnd).getBytes("UTF-8")); //"|"  
        
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                
        // Responses from the server (code and message)
        int serverResponseCode       = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();
        
        Log.e("serverResponseCode Message", Integer.toString(serverResponseCode) + " " + serverResponseMessage);
        
        outputStream.flush();
        outputStream.close();
        
        String responseUTF = "error";
        
        //----------- response ---------
        try {
          InputStream in = connection.getInputStream(); 

          //read it with BufferedReader
            BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            StringBuilder sb = new StringBuilder();
       
            String line;
            while ((line = br.readLine()) != null) {
              sb.append(line);
            } 
            String response = sb.toString();
            
            responseUTF=  response;
        } catch (IOException e) {
          Log.e(Constants_API.TAG, "Upload_Data:SendRegistrStreaming:"+ e.getMessage()); 
        }
        
        connection.disconnect(); // Let's practice good hygiene
        
        return responseUTF;
      }
      catch (Exception ex){
        Log.e(Constants_API.TAG, "Upload_Data:" + ex.getMessage());
        return "false";
      }
    }
    
    //---------------------------------------------
    //          SENT Comment String 
    //---------------------------------------------
    public static boolean SendCommentStreaming(int IssueID, int UserID, String CommentContent, Context ctx,
            String username, String password     ){

      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;

      String lineEnd    = "\r\n";
      String twoHyphens = "--";
      String boundary   =  "*****";

      try
      {
        String urlSTR = Constants_API.COM_Protocol_S + Constants_API.ServerSTR + 
                    Constants_API.remoteImages +  Phptasks.TASK_COMMENT;
        
        URL url    = new URL( urlSTR  );
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",  "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream(connection.getOutputStream() );
                    
        //---------------- Params ------------
        // 1 Title
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"issueId\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(Integer.toString(IssueID) + lineEnd); 
        
        // 2 userid
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"userid\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(Integer.toString(UserID) + lineEnd);
        
        // 3 Description
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write((CommentContent + lineEnd).getBytes("UTF-8")); //"|"  
        
        //4  UserName 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"username\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(username + lineEnd);
        
        // 5 passwordSTR 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"password\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        String passEnc = Security.EncWrapper(password);
        outputStream.writeBytes( passEnc + lineEnd);
        
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                
        // Responses from the server (code and message)
        int serverResponseCode       = connection.getResponseCode();
        
        Log.d(Constants_API.TAG + " serverResponseCode", " " + serverResponseCode);
        
        String serverResponseMessage = connection.getResponseMessage();
        
        outputStream.flush();
        outputStream.close();
        
        if (serverResponseMessage.equals("OK"))
          return true;
        else 
          return false;
      }
      catch (Exception ex){
        Log.e(Constants_API.TAG, "Upload_Data: SendCommentStreaming:" + ex.getMessage());
        return false;
      }
      
    }// Endof POST/*
    
    
    //=========   SENT ISSUE ====================== 
    /**
     * Submit issue to remote server.
     * 
     * @param ImPATHFN_Source  Image local path
     * @param ImFN_Target      Image path to store on remote server 
     * @param title            Issue title 
     * @param catid            Category of the issue    
     * @param Lat_D            latitude
     * @param Long_D           longitude
     * @param descriptionData_STR  Issue description
     * @param Address_STR          Address in textual format 
     * @param UserNameSTR         
     * @param PasswordSTR
     * @return
     */
    public static boolean SendIssue(String ImPATHFN_Source, String ImFN_Target, 
        String title, int catid, double Lat_D, double Long_D, 
        String descriptionData_STR, String Address_STR, String UserNameSTR, String PasswordSTR     ){

      HttpURLConnection connection = null;
      DataOutputStream outputStream = null;

      String lineEnd    = "\r\n";
      String twoHyphens = "--";
      String boundary   =  "*****";

      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1*1024*1024;

      try  {
        String urlSTR = Constants_API.COM_Protocol_S + Constants_API.ServerSTR +  
                    Constants_API.remoteImages +  Phptasks.TASK_ADD_ISSUE;
        URL url    = new URL( urlSTR  );
        connection = (HttpURLConnection) url.openConnection();

        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        // Enable POST method
        connection.setRequestMethod("POST");

        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        outputStream = new DataOutputStream(connection.getOutputStream() );

        //---------------- Params ------------
        // 1 Title
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"title\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write(("|" +title + lineEnd).getBytes("UTF-8")); 

        // 2 catid
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"catid\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(Integer.toString(catid) + lineEnd);

        // 3 Lat
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"latitude\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(Double.toString(Lat_D) + lineEnd);

        // 4 Long
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"longitude\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(Double.toString(Long_D) + lineEnd);

        // 5 Description
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"description\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write(("|" + descriptionData_STR + lineEnd).getBytes("UTF-8")); 

        // 6 address
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"address\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write(("|" + Address_STR + lineEnd).getBytes("UTF-8")); 


        // 7 UserName 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"username\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(UserNameSTR + lineEnd);

        // 8 passwordSTR 
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"password\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        String passEnc = Security.EncWrapper(PasswordSTR);
        outputStream.writeBytes( passEnc + lineEnd);

        //---------------- Image --------------
        if (ImFN_Target.length()>0){
          outputStream.writeBytes(twoHyphens + boundary + lineEnd);
          outputStream.writeBytes("Content-Disposition: form-data; " +
              "name=\"jform[photo]\";filename=\"" + ImFN_Target + "\"" + lineEnd  +"Content-Type: image/jpeg" +  lineEnd);
          outputStream.writeBytes(lineEnd);

          FileInputStream fileInputStream = new FileInputStream(new File(ImPATHFN_Source));

          bytesAvailable = fileInputStream.available();


          bufferSize = Math.min(bytesAvailable, maxBufferSize);
          buffer = new byte[bufferSize];

          // Read file
          bytesRead = fileInputStream.read(buffer, 0, bufferSize);
          while (bytesRead > 0)
          {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
          }
          fileInputStream.close();

        } 

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // Responses from the server (code and message)
        int serverResponseCode       = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();

        Log.e("serverResponseCode", " " + serverResponseCode);
        
        outputStream.flush();
        outputStream.close();

        if (serverResponseMessage.equals("OK"))
          return true;
        else 
          return false;

      }
      catch (Exception ex){
        Log.e(Constants_API.TAG, "Upload_Data:SendIssue:" + ex.getMessage());
        return false;
      }

    }// Endof Image Sent/*
}




Java Source Code List

com.mk4droid.IMC_Activities.Activity_Information_Detailed.java
com.mk4droid.IMC_Activities.Activity_Register.java
com.mk4droid.IMC_Activities.Activity_Setup.java
com.mk4droid.IMC_Activities.Activity_Splash_Login.java
com.mk4droid.IMC_Activities.Activity_Thinking.java
com.mk4droid.IMC_Activities.FActivity_TabHost.java
com.mk4droid.IMC_Activities.Fragment_Comments.java
com.mk4droid.IMC_Activities.Fragment_Filters.java
com.mk4droid.IMC_Activities.Fragment_Issue_Details.java
com.mk4droid.IMC_Activities.Fragment_List.java
com.mk4droid.IMC_Activities.Fragment_Map.java
com.mk4droid.IMC_Activities.Fragment_NewIssueA.java
com.mk4droid.IMC_Activities.Fragment_NewIssueB.java
com.mk4droid.IMC_Activities.package-info.java
com.mk4droid.IMC_Constructors.Category.java
com.mk4droid.IMC_Constructors.Comment.java
com.mk4droid.IMC_Constructors.IssueListItem.java
com.mk4droid.IMC_Constructors.IssuePic.java
com.mk4droid.IMC_Constructors.Issue.java
com.mk4droid.IMC_Constructors.VersionDB.java
com.mk4droid.IMC_Constructors.Vote.java
com.mk4droid.IMC_Constructors.package-info.java
com.mk4droid.IMC_Core.FilterCateg_ExpandableListAdapter.java
com.mk4droid.IMC_Core.ImageView_Zoom.java
com.mk4droid.IMC_Core.InfoWindowAdapterButtoned.java
com.mk4droid.IMC_Core.Issues_ListAdapter.java
com.mk4droid.IMC_Core.Preference_About.java
com.mk4droid.IMC_Core.Preference_AccountOperations.java
com.mk4droid.IMC_Core.Preference_Email.java
com.mk4droid.IMC_Core.Preference_PlainText.java
com.mk4droid.IMC_Core.Preference_Reset.java
com.mk4droid.IMC_Core.Preference_SeekBar.java
com.mk4droid.IMC_Core.SpinnerAdapter_NewIssueCateg.java
com.mk4droid.IMC_Core.package-info.java
com.mk4droid.IMC_Services.DatabaseHandler.java
com.mk4droid.IMC_Services.Download_Data.java
com.mk4droid.IMC_Services.InternetConnCheck.java
com.mk4droid.IMC_Services.Security.java
com.mk4droid.IMC_Services.Service_Data.java
com.mk4droid.IMC_Services.Service_Location.java
com.mk4droid.IMC_Services.Upload_Data.java
com.mk4droid.IMC_Services.package-info.java
com.mk4droid.IMC_Store.Constants_API.java
com.mk4droid.IMC_Store.Phptasks.java
com.mk4droid.IMC_Store.package-info.java
com.mk4droid.IMC_Utils.GEO.java
com.mk4droid.IMC_Utils.My_Crypt_Utils.java
com.mk4droid.IMC_Utils.My_Date_Utils.java
com.mk4droid.IMC_Utils.My_System_Utils.java
com.mk4droid.IMC_Utils.RestCaller.java
com.mk4droid.IMC_Utils.RestClient.java
com.mk4droid.IMC_Utils.package-info.java