Showing posts with label retrofit. Show all posts
Showing posts with label retrofit. Show all posts

Wednesday, November 8, 2017

Insert Image From Android To Database (PHP + MySQL)

This article will show a simple tutorial about how to insert image from android to database. Insert image from android to database possible achieve by creating local database using PHP and MySQL.

So this tutorial using two projects, 1 PHP project and the other one is Android project. Don't forget to create your database to store the image that received from Android.

Image will be send from android by send its json to server. In php project, we also create a file that handle json from mobile (android).

Scenario 


We will save the file of image in the directory inside the php project. In database we just store the path of the image from php project.

For example
  • Users choose image obama.jpg from their android
  • In PHP project, there is a directory named imagefromandroid.
  • In database, just store its path, so it's become : imagefromandroid/obama.jpg.

Create Database


In this project i just create simple database, only one table. Please adjust your project and your database.

Database

PHP Project


Create a directory that used to save the file of image that received from android. Take a look at the following image

Directory

I above figure, i create a directory where image from mobile android will stored name imagefromandroid. Also, we will use two php classes, Connection.php and insertImageFromMobile.php.

Connection.php


This file used to connect your PHP Project to your database. The code look like the follows :

InsertImageFromMobile.java


This class used to handle data that received from mobile in json form. The code is look like the follows :


Android Project


There are several classes and xml files that we create in order to able to send image to the server or database.

LoggingInterceptors.java


This class created for handle response of retrofit. In other word, we able to see request process of retrofit to the endpoint.


ResultModel.java


This class will handle response message of request. The response message that will shown by this class are success or failed.



ApiService.java


This class is an interface. In this class will declare endpoint of BASE URL.


ApiClient.java



This class contains the BASE URL that used in this project. And also in this class contains initialized of the retrofit

MainActivity.java


All class or method that used to send image to the server will called in this class. Full code of MainActivity :


Okay, that's all. Hope it helps you to learn how to insert image from android to database.

Wednesday, August 23, 2017

Android Adding File Certificate To Retrofit 2.0+

After the previous post about how to adding file certificate to Retrofit 1.7, today i gonna share a new tutorial about how to adding file certificate to Retrofit 2.0.


As you know, one of usage of file certificate is used to access HTTPS from HTTP. If your current host is HTTPS but you still access using HTTP , so by using file certificate it will be redirecting to HTTPS automatically.

Before you adding file certificate to Retrofit 2.0 make you using OKHTTP 3 for your client. So here is the method that you can use for adding file certificate to Retrofit 2.0 :

First import the following package :

Here the method that i use in my project, write the following method (you can modify the method as you wish) :

That's all, hope it helps and works. Thank you.

Tuesday, August 22, 2017

Android Add File Certificate To Retrofit 1.7

In current  project that you develop from the beginning you must be use the latest version of the library, for example library Retrofit.



In several case, you must be face a difficulty in Retrofit 1.7 how to add file certificate to Retrofit 1.7, right?

Because of this, i create this article and share my experience when i add file certificate to my apps using Retrofit 1.7

If you using Retrofit 1.7, make sure another support libraries are added, these libraries that i meant are :

compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'

After that, write to the following code in your apps to adding file certificate to Retrofit 1.7 :

Above is some code snippet in my project. I don't how the model of your code, but you can modify my code above and implement in your project. If you are using Retrofit 1.7, then my code will works in your project.

That's all about how to adding file Certificate to Retrofit 1.7, hope it helps and works in your project. Thank you.

Thursday, January 26, 2017

Retrofit Android Insert Data To Database (Server Using : PHP + MySQL)

In previous posts, I have created a tutorial that show process input data to server using dummy server (apiary), means there is no data that inserted do server. Apiary was used just for testing the process of the application.



I my previous tutorial that I created using apiary as server just focused to explain about method GET and POST on Retrofit, how to send data using method GET or POST. There is no PHP file.

But, today I going to write article about how to input data to database from android application. The database using MySQL (Localhost/phpMyAdmin) and the server made from PHP.

To transfer data to server, on Android I using external library call retrofit 2.0. Retrofit will helps us to send data to database.

In PHP file, I uses PDO as the way to connecting between PHP and database (MySQL)

Creating Database MySQL


The first thing that we have to do is creating database. You can create the database MySQL from anywhere, from example from HeidiSQL, MySQL Server or PhphMyAdmin. In this case I uses PhpMyAdmin to creating database.

Recommeded : Android Create, Read, Update, Delete data in Firebase Firestore

1. Open localhost/phpmyadmin (make sure your xampp has been on)

2. Then you will redirected to the page that looks like the following Figure 1. To create database, choose New (menu in Yellow)

Figure 1

3. After that, write your database name and choose the type of the database. Take a look at the following Figure 2

Figure 2

Create database name as : loginphpandroid
Choose the type : utf8_general_ci

4. After your database done, now create the Table. To create the Table click menu new below your database name. Take a look at Figure 3 below

Figure 3
After you click the menu new (menu in yellow), the you will redirecting to the page where you can write your Table's name.

5. To create name of your Table, please take a look at the Figure 4

Figure 4
Guidelines of  Figure 4 :
  • Give the name of the Table as : Food
  • Choose number of row in table Food
  • Then choose Go

6.  After the table has done, now create the attributes/row in Table Food. Take a look at the following Figure 4.

Figure 5

Guideline of Figure 5 :
  • Give name for the first attribute as idfood, its type :  INT
  • Set idfood as Primary Key
  • Because the attribute idfood is primary key, then checklist A_I (Auto Increment).
  • Give name for the second attribute as foodname, its type VARCHAR
  • Give name for the third attribute as foodqty, its type INT
  • If done, then choose Save

Oke the database is ready to use. Now time to configure code of the PHP and Android project.

PHP Code


Recommended : Create rating and review look like Google Play Store Android

On PHP project that I created contains several PHP classes. Among that PHP files, I placed on different package. For clearly about the folder structure of the project, just take a look at the following figure 6

Figure 6


1. Connection.php

This file used to connecting php project that created to the database. If connected, then we allowed to input data either from Mobile or Form HTML. Below is the full code of Connection.php  :

2. InsertFood.php

This file used to insert data that get from users to the database. In this data gotten from Mobile Android. Below is the full code of InsertFood.php :

Attention! There are several codes that I have to explain:

$response = array()

The code above created for save the response in json form.

//cek is the row was inserted or not 
if($sqlInsert){     
    //success inserted     
    $response["success"] = 1;     
    $response["message"] = "Food successful inserted!";
    echo json_encode($response);
}else{     
    //failed inserted     
    $response["success"] = 0;     
    $response["message"] = "Failed while insert data";
    echo json_encode($response);}

The code above will do checking. Is the input process has success or not. If success the response will like the follows :
  • success = 1
  • message = Food successful inserted!
When program do echo json_encode($rensponse), then it will displaying output json like the follows :

{"success":1,"message":"Food successful inserted!"}

If the process if falied, the the response that will like the follows :
  • success = 0
  • message = "Failed while insert data"
So when program do echo json_encode($response), the output json will display like the follows :

{"success":0,"message":"Failed while insert data"}

That json response will used in mobile, and will displaying on the Toast message.

Android Code


Recommended : Simply Login Using Firebase UI (No need API anymore)

In this project I using Retrofit 2.0 to help this project send or receive data from android to server.

1. Library

As always the first thing you do before continue project is compile the libraries that needed in the project. In this project compile the following libraries :

//add the following libraries 
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2' 
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

2. Android Manifest

On AndroidManifest add user permission for internet, look like the follows :

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

3. Layout

There are several layout that used in this project :
  • activity_main.xml
  • popup_insert_food.xml
Create layout activity_main.xml, then paste the following code :

Next create layout popup_insert_food.xml. This layout contains two Edit Text for foodName dan foodQty. In its process this layout will appears on the popup. Paste the following code :


4. Java classes

There are several java classes that need in order to do insert data to the database, there are :
  • ApiService.java
  • ApiClient.java
  • LoggingInterceptors.java
  • InsertFoodResponseModel.java
  • MainActivity.java
This interface contains method that used to send data to server. Paste the following code :

This class contains the URL that used in this project. And also in this class contains initialized of the retrofit. Paste the following code :

Take a look at the URL. 192.168.43.147 is IP from my computer. And AndroidPhpLogin my project folder that located in localhost (htdocs). Change the IP/URL and the project folder name with your own.

This class used to save the response of the json from server (Take a look that the json that i have explained above in php code). Paste the following code :

Create class LoggingInterceptors.java. This class used to see the output of the input process to the server.

Why use this class? Because in retrofit we will face a little bit difficulty to see the result, especially the URL and the JSON. You can see the result on logcat. The full code on LoggingInterceptor.java is like the follows  :

Last is MainActivity.java. All the process and above classes will unite in this class. The full code of MainActivity.java is like the follows :

Demikian beberapa langkah yang dapat saya sampaikan tentang cara meng input data dari Android ke Database menggunakan Server yang dibuat dari Native PHP.
That's all about the steps how to input data from android to server made from PHP and MySQL.

If there are unclear about the steps above, feel-free to asked me. Thank you.

Download the project by clicking the following image


Thursday, December 29, 2016

Example Tutorial Retrofit 2.0 Method GET Using Parameter

Hello everyone, today i going to share a nice tutorial about how to use method GET of Retrofit 2.0. I will get my profile by parameter.



Example :
My profile's ID is 1. So the url become www.url.com/endpoint?paremeter=1
In this example i just use one data profile with ID = 1. You can improve your project by creating much profile.

As you can see at the video above, after success get data profile, the response will display all attribute of my profile like ID, NAME, JOB and AGE

PREPARATION

First, in order we to able to use Retrofit 2.0, we have you compile the following libraries in your dependencies build.gradle (Module: app) :

// retrofit 2.0
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Add uses-permission in your AndroidManifest.xml like the following permission :

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

IMPLEMENTATION

First, create an interface that define Method GET of Retrofit that we'll use to get data profile. Give it name as ApiService.java. Paste the following code :

On code above, "myself" is the endpoint. Endpoint is a attribute after url (ww.url.com/endpoint). @Query("id") used to define the parameter


Second, create new java class, give it name as LoggingInterceptor.java. Paste the following code :

This java class help us to set the output of process get data profile. You can see the output on the android studio logcat.


Third, create new java class, give it name as ApiClient.java, in this class we define the URL, set the LoggingInterceptor and client. Paste the following code :


Forth, create new java class, give it name as ProfileModel.java. This class will used to store the response of the JSON. Paste the following code :

The attribute of @SerializedName("...") must same as the attribute in the JSON. Example in the JSON write as "id", the serializedName become to @SerializedName("id")


Last, create layout and java class of MainActivity.

activity_main.xml, paste the following code :

MainActivity.java, paste the following code :

Done! Run your project, and hope it running well.

Tuesday, December 27, 2016

POST Method Using Retrofit 2.0 [Example case : Login Process]

Today i going to share you about how to POST data using retrofit 2.0. The example case that i use here is login process until get the success response.



How the steps? What should be prepared? Let's start!


Compile the several libraries below in your build.gradle (apps):

compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

In your AndroidManifest.xml, add uses permission look like the following:

Create the several layouts :
  1. activity_main.xml > Display the main page
  2. item_answer.xml > Display the aswers of FAQ
  3. item_question.xml > Display the questions of FAQ
Create the several java classes :
  1. MainActivity.java > All the login process and passing value will done here
  2. ApiClient.java > Create the Retrofit's client and interceptors
  3. Inteface ApiService.java > Initialized of retrofit's POST method
  4. LoggingInterceptors.java > To help you see the output of the process in Logcat
  5. FaqModel.java > To store the response of Questions and answers
  6. ListFaqModel.java > To store  list of FAQ
  7. ExpandAdapter.java > To set questions and answers on exapandable

Why we need LoggingInterceptors.java? Because in retrofit we'll difficult to detect the output of the process. If we find an error, it will be hard to debug. So we need this class :

You have to know how the form of the JSON of your target. It used to make us easy to create its model.

Your URL Target :

http://private-fc41f-myprofile2.apiary-mock.com/loginsaya

With required field username dan password

The JSON of the response after you success do login :

Now time to create time the code of the Layouts.

Create new xml file and give it name as activity_main.xml. Paste the following code :

Create new xml file and give it name as item_answer.xml. Paste the following code:

Create new xml file and give it name as item_questions.xml. Paste the following code :

Next, Create all the java classes.

Create new java file and give it name as ApiClient.java. Paste the following code :

Create new java file and give it name as ApiService.java. Paste the following code :

Create new java file and give it name as LoggingInterceptor.java. Paste the following code :

Create new java class that will store the object of FAQ, give it name as FaqModel.java. Paste the following code :

Because the form of the API required List to cover FaqModel.java, so we must create a new class called ListFaqModel.java. Paste the following code :

After that, create new adapter class. This class will set the the data to the model. Give it name as ExpandAdapter.java. Paste the following code :

Next is MainActivity.java, paste the following code :

If has done, run your project. Hope it will works well. I have tried before i post here and it running well.

*****
because we use LoggingInterceptors.java, so we can see the process on your Android Studio's logcat.  :


You can clone the real project here GITHUB

OK, That's all about how to do POST using retrofit 2.0. Hope it helps.