Skip to content

Commit

Permalink
Add upload file supporting
Browse files Browse the repository at this point in the history
  • Loading branch information
ABPSoft committed Dec 26, 2018
1 parent 3b02ef0 commit f5b1293
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 16 deletions.
6 changes: 3 additions & 3 deletions ABPWebService/build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 11
versionName "1.1"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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()));

Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile project(path: ':ABPWebService')
implementation project(path: ':ABPWebService')
}
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aminbahrami.abpwebservicelib">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -12,7 +16,7 @@
<activity android:name=".ActivityMain">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -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());
}
}
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
android:layout_height="match_parent"
tools:context=".ActivityMain">

<TextView
<Button
android:id="@+id/sendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
android:text="Send Request"/>

</LinearLayout>

0 comments on commit f5b1293

Please sign in to comment.