不使用php和android保存选定的微调器值

时间:2016-01-09 07:24:40

标签: java php android mysql android-spinner

在我的项目中,我正在使用一个带有3个edittexts字段的微调项目。我将这些值通过url发送到php文件,而php文件又将这些值存储到数据库...如果我只使用在我的程序中有三个edittext字段,它存储到数据库中。现在我的问题是,所选择的微调器值没有保存到数据库中。以前只有三个edittext值正在保存。使用spinner之后没有任何东西保存到数据库.. 这里我想发送三个编辑文本值和选择的微调项目作为php文件的参数。 请帮我将选定的微调项目保存到数据库..

这是我的文件..

MainActivity.java

package com.example.server;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({ "deprecation", "unused" })
public class MainActivity extends Activity {

    private Spinner spin;
    private EditText editTextFname;
    private EditText editTextLname;
    private EditText editTextMobile;
    //private EditText editTextPin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spin = (Spinner)findViewById(R.id.spinner1);
        editTextFname = (EditText) findViewById(R.id.editTextFname);
        editTextLname = (EditText) findViewById(R.id.editTextLname);
        editTextMobile = (EditText) findViewById(R.id.editTextMobile);
        //editTextPin = (EditText) findViewById(R.id.editTextPin);
        ArrayAdapter<CharSequence> spinner = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
        spinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(spinner);

    }

    public void insert(View view){

        String salutation = spin.getSelectedItem().toString();//spinner item..not saving..
        String fname = editTextFname.getText().toString();
        String lname = editTextLname.getText().toString();
        String mobile = editTextMobile.getText().toString();


        insertToDatabase(salutation,fname,lname,mobile);


        /*Uri uri = Uri.parse("http://oursite/insert-db.php"); 
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);*/
    }

    private void insertToDatabase(String salutation, String fname, String lname,String mobile){
        class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
            @SuppressWarnings({ "unused", "deprecation" })
            @Override
            protected String doInBackground(String... params) {
                @SuppressWarnings("unused")
                String paramSalutation = params[0];
                String paramFname = params[1];
                String paramLname = params[2];
                String paramMobile = params[3];

                //InputStream is = null;

                String salutation = spin.getSelectedItem().toString();
                String fname = editTextFname.getText().toString();
                String lname = editTextLname.getText().toString();
                String mobile = editTextMobile.getText().toString();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("salutation", salutation));
                nameValuePairs.add(new BasicNameValuePair("fname", fname));
                nameValuePairs.add(new BasicNameValuePair("lname", lname));
                nameValuePairs.add(new BasicNameValuePair("mobile", mobile));

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(
                            "http://oursite/insert-db.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    //is = entity.getContent();


                } catch (ClientProtocolException e) {

                } catch (IOException e) {

                }
                return "success";
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                TextView textViewResult = (TextView) findViewById(R.id.textViewResult);
                textViewResult.setText("Inserted");
            }
        }
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(salutation,fname,lname,mobile);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

插入-db.php中

<?php

    define('HOST','mysql.hostinger.in');
    define('USER','u449667615_user');
    define('PASS','1234');
    define('DB','u449667615_db');

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

  $salutation = $_POST['salutation'];   
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $mobile = $_POST['mobile'];


   $sql = "insert into person (salutation,fname,lname,mobile) values ('$salutation','$fname','$lname','$mobile')";
  if(mysqli_query($con,$sql)){
    echo 'success';
  }
  else{
    echo 'failure';
  }
  mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

检查spin.getSelectedItem().toString()返回值。 同时尝试在echo移动echo $_POST['salutation'];到最后位置的<?php 值,因为如果失败则会使以下回声崩溃。