انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
ساخت پروژه تماس با ما در آندروید + PHP + سورس پروژه

ساخت پروژه تماس با ما در آندروید + PHP + سورس پروژه

#1
سلام
در این پروژه یک فرم تماس با ما درست میکنم تا کاربرای اپلیکیشن بتونن نظراتشون رو  ارسال کنن و این نظرات در دیتابیس mysql با کمک PHP ذخیره بشه :

دمو پروژه : http://www.aparat.com/v/f1Zpm

فایل main.xml:

کد پی‌اچ‌پی:
<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:layout_margin="15dp"
    
tools:context="co.tooba.contactus.Main" >

    <
TextView
        android
:id="@+id/textView1"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignParentRight="true"
        
android:layout_marginRight="17dp"
        
android:layout_marginTop="20dp"
        
android:text="نام"
        
android:textAppearance="?android:attr/textAppearanceSmall" />

    <
TextView
        android
:id="@+id/TextView01"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignRight="@+id/textView1"
        
android:layout_below="@+id/textView1"
        
android:layout_marginTop="20dp"
        
android:text="ایمیل"
        
android:textAppearance="?android:attr/textAppearanceSmall" />

    <
TextView
        android
:id="@+id/TextView02"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignRight="@+id/TextView01"
        
android:layout_below="@+id/TextView01"
        
android:layout_marginTop="20dp"
        
android:text="شماره تماس"
        
android:textAppearance="?android:attr/textAppearanceSmall" />

    <
TextView
        android
:id="@+id/TextView03"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignRight="@+id/TextView02"
        
android:layout_below="@+id/TextView02"
        
android:layout_marginTop="20dp"
        
android:text="پیام"
        
android:textAppearance="?android:attr/textAppearanceSmall" />

    <
EditText
        android
:id="@+id/editName"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_above="@+id/TextView01"
        
android:layout_toLeftOf="@+id/TextView02"
        
android:ems="10" >

        <
requestFocus />
    </
EditText>

    <
EditText
        android
:id="@+id/editEmail"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignBottom="@+id/TextView01"
        
android:layout_alignLeft="@+id/editName"
        
android:layout_toLeftOf="@+id/TextView02"
        
android:ems="10" />

    <
EditText
        android
:id="@+id/editPhone"
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_alignBottom="@+id/TextView02"
        
android:layout_alignRight="@+id/editEmail"
        
android:ems="10" />

    <
EditText
        android
:id="@+id/editMessage"
        
android:layout_width="wrap_content"
        
android:layout_height="150dp"
        
android:layout_alignLeft="@+id/editPhone"
        
android:layout_alignRight="@+id/TextView03"
        
android:layout_below="@+id/TextView03"
        
android:layout_marginTop="18dp"
        
android:ems="10"
        
android:gravity="top"
        
android:inputType="textMultiLine" />

    <
Button
        android
:id="@+id/btnSend"
        
style="?android:attr/buttonStyleSmall"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignLeft="@+id/editMessage"
        
android:layout_below="@+id/editMessage"
        
android:layout_centerHorizontal="true"
        
android:layout_marginTop="14dp"
        
android:text="ارسال" />

</
RelativeLayout

فایل Main.jav :

کد پی‌اچ‌پی:
package co.tooba.contactus;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class 
Main extends Activity {

    private 
EditText name ,email ,phone ,message ;
    private 
Button send;
    private 
String url ;
    
    protected 
void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.main);
        
        
send = (Button)findViewById(R.id.btnSend);
        
        
name = (EditText)findViewById(R.id.editName);
        
email = (EditText)findViewById(R.id.editEmail);
        
phone = (EditText)findViewById(R.id.editPhone);
        
message = (EditText)findViewById(R.id.editMessage);
        
        
        
send.setOnClickListener(new OnClickListener() {
            public 
void onClick(View arg0) {

                
url "http://192.168.0.152/contact_us.php?name="+name.getText()+"&email="+email.getText()+"&message="+message.getText()+"&phone="+phone.getText();
                
Server db = new Server(Main.this url);
                
db.execute();
                
            }
        });
                
    }



فایل Server.java :

کد پی‌اچ‌پی:
package co.tooba.contactus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class 
Server extends AsyncTask<StringStringString> {

    private 
ProgressDialog dialog ;
    private 
Context myContext;
    private 
InputStream data null;
    private 
String url ;
    private 
String output "";
    
    public 
Server (Context context String Url){
        
myContext context;
        
url Url;
    }
    
    
    @
Override
    
protected void onPreExecute() {
        
super.onPreExecute();
        
        
dialog = new ProgressDialog(myContext);
        
dialog.setTitle("ارتباط با سرو");
        
dialog.setMessage("در حال ارسال اطلاعات ...");
        
dialog.setIndeterminate(false);
        
dialog.setCancelable(true);
        
dialog.show();    
        
    }
    
    @
Override
    
protected String doInBackground(String... arg0) {
        
        try {

            
DefaultHttpClient httpClient = new DefaultHttpClient();
            
HttpGet httpGet = new HttpGet(url);
            
HttpResponse response httpClient.execute(httpGet);
            
HttpEntity entity response.getEntity();
            
data entity.getContent();

            
        } catch (
ClientProtocolException e) {
            
e.printStackTrace();
        } catch (
IOException e) {
            
e.printStackTrace();
        }
        
        
        try {
            
BufferedReader reader = new BufferedReader( new InputStreamReader(data "UTF-8"), 8);
            
StringBuilder sb = new StringBuilder();
            
String row null;
            
            while ((
row reader.readLine()) != null) {
                
sb.append(row "\n");
            }
            
data.close();
            
output sb.toString();
            
        } catch (
UnsupportedEncodingException e) {
            
e.printStackTrace();
        } catch (
IOException e) {
            
e.printStackTrace();
        }
        
        return 
output;
    }
    
    @
Override
    
protected void onPostExecute(String output) {
            
dialog.dismiss();
        try {
            
Toast.makeText(myContextoutputToast.LENGTH_LONG).show();
        } catch (
Exception e) {
            
e.printStackTrace();
        }
   }



فایل PHP :

کد پی‌اچ‌پی:
<?php
$con 
mysql_connect('localhost','root','');
mysql_select_db('contact_us',$con);
mysql_query("SET NAMES 'utf8'");

    if(! empty(
$_GET['name'])){

        if(!
filter_var($_GET['email'], FILTER_VALIDATE_EMAIL))
            echo 
"ایمیل را به صورت صحیح وارد کنید.";
        elseif(empty(
$_GET['message']))
            echo 
"فیلد پیام خالی می باشد.";
        else{
                
$name $_GET["name"];
                
$email $_GET["email"];
                
$phone = (isset($_GET["phone"])) ? $_GET["phone"] : "";
                
$message $_GET["message"];
                
$result mysql_query("INSERT INTO messages(name, email, phone, message) VALUES ('$name' , '$email' , '$phone' , '$message')");

                if(
$result)
                    echo 
"پیام شما با موفقیت ارسال شد.";
        }
    }else{
        echo 
"فیلدها را به دقت تکمیل کنید.";
    }

?>
 

سعی شده تا به صورت کاملا ساده و بدون هیچ کد اضافه ای این پروژه رو براتون آماده کنیم . لذا حواستون باشه که در قسمت امنیت php کاری انجام نشده است و این پروژه جهت آموزش می باشد .- در لاین 31 فایل Main.java آدرس قرارگیری فایل PHP را باید مشخص کنید تا اطلاعات و دریافت اطلاعات به صورت صحیح انجام شود .– ارسال اطلاعات از طریق HttpGet به فایل PHP ارسال میشود و پیام مورد نظر از  طریق Toast در اکتیویتی نمایش داده میشود .

لینک دانلود سورس پروژه :

http://blog.toobaweb.com/wp-content/uplo...tactUs.zip


فایل‌های پیوست

دانلود ContactUs.zip

نام فایل ContactUs.zip
نوع فایل .zip
دفعات دانلود 358
اندازه 2.3 MB
ارسال کننده فایل Ali Majidi
پاسخ
 سپاس شده توسط admin ، so2011 ، داش بهروز ، ramtin2080 ، mortaza7khat


پیام‌های این موضوع
ساخت پروژه تماس با ما در آندروید + PHP + سورس پروژه - توسط Ali Majidi - ۱۳۹۴/۰۶/۲۳, ۰۲:۰۱ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان