From f5b1293cb9922c02173b5acd21d50785020996aa Mon Sep 17 00:00:00 2001 From: Amin Bahrami Date: Wed, 26 Dec 2018 11:23:48 +0330 Subject: [PATCH] Add upload file supporting --- ABPWebService/build.gradle | 6 +- .../abpwebservice/ABPWebService.java | 101 ++++++++++++++++-- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 6 +- .../abpwebservicelib/ActivityMain.java | 41 +++++++ app/src/main/res/layout/activity_main.xml | 5 +- 6 files changed, 145 insertions(+), 16 deletions(-) diff --git a/ABPWebService/build.gradle b/ABPWebService/build.gradle index cbb1800..441da0c 100644 --- a/ABPWebService/build.gradle +++ b/ABPWebService/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' -group='com.aminbahrami' +group = 'com.aminbahrami' android { compileSdkVersion 28 @@ -10,8 +10,8 @@ android { defaultConfig { minSdkVersion 17 targetSdkVersion 28 - versionCode 1 - versionName "1.0" + versionCode 11 + versionName "1.1" } buildTypes { diff --git a/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java b/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java index d1d04d3..197a438 100644 --- a/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java +++ b/ABPWebService/src/main/java/com/aminbahrami/abpwebservice/ABPWebService.java @@ -1,9 +1,13 @@ package com.aminbahrami.abpwebservice; import android.os.Handler; +import android.util.Base64; import android.util.Log; import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; @@ -45,17 +49,36 @@ public void setOnNetwork(IOnNetwork iOnNetwork) this.iOnNetwork=iOnNetwork; } - public void sendRequest(final String inputName,final String message) + public void sendRequest(String data) { - Log.i("ABPWebService","Request: "+message); + sendRequest(null,data); + } + + public void sendRequest(String inputName,String data) + { + sendRequest(inputName,data,null,null); + } + + public void sendRequest(final String inputName,final String data,final String fileInputName,final File file) + { + Log.i("ABPWebService","Request: "+data); Thread thread=new Thread(new Runnable() { @Override public void run() { - //String requestData=inputName+"="+message; - String requestData=message; + String requestData; + + if(inputName==null) + { + requestData=data; + } + else + { + requestData=inputName+"="+data; + } + URL apiUrl=null; @@ -68,15 +91,72 @@ public void run() httpURLConnection.setConnectTimeout(connectTimeout); httpURLConnection.setReadTimeout(readTimeout); - httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); + if(inputName==null) + { + httpURLConnection.setRequestProperty("Content-Type","application/json; charset=UTF-8"); + } httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); - OutputStreamWriter osw=new OutputStreamWriter(httpURLConnection.getOutputStream(),"UTF-8"); - osw.write(requestData); - osw.flush(); + DataOutputStream dataOutputStream=null; + + if(file!=null) + { + String boundary="***************"; + + String lineEnd="\r\n"; + String twoHyphens="--"; + + FileInputStream fileInputStream=new FileInputStream(file); + httpURLConnection.setUseCaches(false); + httpURLConnection.setRequestProperty("ENCTYPE","multipart/form-data"); + httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary="+boundary); + + + String content="Content-Disposition: form-data; name=\""+inputName+"\""+lineEnd+lineEnd+data+lineEnd+lineEnd; + content+=twoHyphens+boundary+lineEnd+"Content-Disposition: form-data; name=\""+fileInputName+"\";filename=\""+file.getName()+"\""+lineEnd; + + dataOutputStream=new DataOutputStream(httpURLConnection.getOutputStream()); + dataOutputStream.writeBytes(twoHyphens+boundary+lineEnd); + dataOutputStream.writeBytes(content); + dataOutputStream.writeBytes(lineEnd); + + int byteAvailable, bufferSize, bytesRead; + byte[] buffer; + int maxBufferSize=1024*1024; + + byteAvailable=fileInputStream.available(); + bufferSize=Math.min(byteAvailable,maxBufferSize); + + buffer=new byte[bufferSize]; + bytesRead=fileInputStream.read(buffer,0,bufferSize); + + while(bytesRead>0) + { + dataOutputStream.write(buffer,0,bufferSize); + byteAvailable=fileInputStream.available(); + bufferSize=Math.min(byteAvailable,maxBufferSize); + + bytesRead=fileInputStream.read(buffer,0,bufferSize); + } + + dataOutputStream.writeBytes(lineEnd); + dataOutputStream.writeBytes(twoHyphens+boundary+twoHyphens+lineEnd); + } + else + { + OutputStreamWriter osw=new OutputStreamWriter(httpURLConnection.getOutputStream(),"UTF-8"); + osw.write(requestData); + osw.flush(); + } + + if(dataOutputStream!=null) + { + dataOutputStream.close(); + dataOutputStream.flush(); + } BufferedReader br=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); @@ -91,7 +171,10 @@ public void run() Log.i("ABPWebService","Response: "+strAll); - iOnNetwork.onResponse(strAll.toString()); + if(iOnNetwork!=null) + { + iOnNetwork.onResponse(strAll.toString()); + } } catch(MalformedURLException e) { diff --git a/app/build.gradle b/app/build.gradle index ab7bfcb..c7970c5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,5 +19,5 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - compile project(path: ':ABPWebService') + implementation project(path: ':ABPWebService') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 85e7399..72a0af5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,10 @@ + + + + - + diff --git a/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java b/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java index ab09529..bcd380a 100644 --- a/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java +++ b/app/src/main/java/com/aminbahrami/abpwebservicelib/ActivityMain.java @@ -2,9 +2,17 @@ import android.app.Activity; import android.os.Bundle; +import android.os.Environment; +import android.util.Log; +import android.view.View; import com.aminbahrami.abpwebservice.ABPWebService; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.File; + public class ActivityMain extends Activity { @@ -13,5 +21,38 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + findViewById(R.id.sendRequest).setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + sendRequest(); + } + }); + } + + private void sendRequest() + { + ABPWebService abpWebService=new ABPWebService(); + abpWebService.setUrl("http://192.168.1.2/test/androidUploadFile/upload.php"); + + //Check Permission in the feature + File file=new File(Environment.getExternalStorageDirectory()+"/test.jpg"); + + Log.i("LOG","FilePath: "+file); + + JSONObject object=new JSONObject(); + try + { + object.put("test","Hello"); + object.put("age",28); + } + catch(JSONException e) + { + e.printStackTrace(); + } + + abpWebService.sendRequest(object.toString()); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 513f4fa..8ba24d9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,9 +6,10 @@ android:layout_height="match_parent" tools:context=".ActivityMain"> - + android:text="Send Request"/> \ No newline at end of file