Posted by : Unknown
Rabu, 06 Desember 2017
Note : Please add internet permission to your
AndroidManifest.xml file.
AndroidManifest.xml file.
<uses-permission
android:name="android.permission.INTERNET"/>
android:name="android.permission.INTERNET"/>
Note: If you are facing The import
org.apache.http.HttpResponce cannot resolved
error solution then read my this tutorial for solve
this problem.
Code for MainActivity.java file.
org.apache.http.HttpResponce cannot resolved
error solution then read my this tutorial for solve
this problem.
Code for MainActivity.java file.
package com.melki.droid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText editTextName, editTextEmail,
editTextWebsite;
String GetName, GetEmail, GetWebsite;
Button buttonSubmit ;
String DataParseUrl = "http://web-anda.com/
insert_data.php";
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = (EditText)findViewById
(R.id.editText1);
editTextEmail = (EditText)findViewById
(R.id.editText2);
editTextWebsite = (EditText)findViewById
(R.id.editText3);
buttonSubmit = (Button)findViewById(R.id.button1);
buttonSubmit.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GetDataFromEditText();
SendDataToServer(GetName, GetEmail, GetWebsite)
;
}
});
}
public void GetDataFromEditText(){
GetName = editTextName.getText().toString();
GetEmail = editTextEmail.getText().toString();
GetWebsite = editTextWebsite.getText().toString();
}
public void SendDataToServer(final String name,
final String email, final String website){
class SendPostReqAsyncTask extends
AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickName = name ;
String QuickEmail = email ;
String QuickWebsite = website;
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair
("name", QuickName));
nameValuePairs.add(new BasicNameValuePair
("email", QuickEmail));
nameValuePairs.add(new BasicNameValuePair
("website", QuickWebsite));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(DataParseUrl);
httpPost.setEntity(new UrlEncodedFormEntity
(nameValuePairs));
HttpResponse response = httpClient.execute
(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Submit Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Data Submit
Successfully", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask =
new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, email,
website);
}
}
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://
schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_
margin"
android:paddingLeft="@dimen/activity_horizontal_
margin"
android:paddingRight="@dimen/activity_horizontal_
margin"
android:paddingTop="@dimen/activity_vertical_
margin"
tools:context="com.submitandroiddata2server_
android_examples.com.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter Your Name" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter Your Email" />
<EditText
android:id="@+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter Your Website" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Submit Data To Server" />
</RelativeLayout>
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText editTextName, editTextEmail,
editTextWebsite;
String GetName, GetEmail, GetWebsite;
Button buttonSubmit ;
String DataParseUrl = "http://web-anda.com/
insert_data.php";
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = (EditText)findViewById
(R.id.editText1);
editTextEmail = (EditText)findViewById
(R.id.editText2);
editTextWebsite = (EditText)findViewById
(R.id.editText3);
buttonSubmit = (Button)findViewById(R.id.button1);
buttonSubmit.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
GetDataFromEditText();
SendDataToServer(GetName, GetEmail, GetWebsite)
;
}
});
}
public void GetDataFromEditText(){
GetName = editTextName.getText().toString();
GetEmail = editTextEmail.getText().toString();
GetWebsite = editTextWebsite.getText().toString();
}
public void SendDataToServer(final String name,
final String email, final String website){
class SendPostReqAsyncTask extends
AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickName = name ;
String QuickEmail = email ;
String QuickWebsite = website;
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair
("name", QuickName));
nameValuePairs.add(new BasicNameValuePair
("email", QuickEmail));
nameValuePairs.add(new BasicNameValuePair
("website", QuickWebsite));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(DataParseUrl);
httpPost.setEntity(new UrlEncodedFormEntity
(nameValuePairs));
HttpResponse response = httpClient.execute
(httpPost);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return "Data Submit Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Data Submit
Successfully", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask =
new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(name, email,
website);
}
}
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://
schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_
margin"
android:paddingLeft="@dimen/activity_horizontal_
margin"
android:paddingRight="@dimen/activity_horizontal_
margin"
android:paddingTop="@dimen/activity_vertical_
margin"
tools:context="com.submitandroiddata2server_
android_examples.com.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter Your Name" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter Your Email" />
<EditText
android:id="@+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:hint="Enter Your Website" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Submit Data To Server" />
</RelativeLayout>
Code for AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://
schemas.android.com/apk/res/android"
package="com.submitandroiddata2server_android_
examples.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission
android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" /
>
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://
schemas.android.com/apk/res/android"
package="com.submitandroiddata2server_android_
examples.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission
android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" /
>
</intent-filter>
</activity>
</application>
</manifest>
Code for insert_data.php file.
<?php
//This script is designed by Android-Examples.com
//Define your host here.
$hostname = "mysql.hostinger.in";
//Define your database username here.
$username = "u727224026_demo";
//Define your database password here.
$password = "1234567890";
//Define your database name here.
$dbname = "u727224026_demo";
$con = mysqli_connect($hostname,$username,$
password,$dbname);
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$Sql_Query = "insert into GetDataTable
(name,email,website) values ('$name','$email','$
website')";
if(mysqli_query($con,$Sql_Query)){
echo 'Data Inserted Successfully';
}
else{
echo 'Try Again';
}
mysqli_close($con);
?>
//This script is designed by Android-Examples.com
//Define your host here.
$hostname = "mysql.hostinger.in";
//Define your database username here.
$username = "u727224026_demo";
//Define your database password here.
$password = "1234567890";
//Define your database name here.
$dbname = "u727224026_demo";
$con = mysqli_connect($hostname,$username,$
password,$dbname);
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$Sql_Query = "insert into GetDataTable
(name,email,website) values ('$name','$email','$
website')";
if(mysqli_query($con,$Sql_Query)){
echo 'Data Inserted Successfully';
}
else{
echo 'Try Again';
}
mysqli_close($con);
?>