从Android应用程序获取数据并使用php将其存储在sql数据库中

时间:2016-09-21 09:32:48

标签: php android phpmyadmin

我想从我的Android应用程序获取数据并放入sql数据库,这是在phpmyadmin(wampp服务器)。 这是我的代码:

package com.example.sara.myapplication;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class addupdel_activity extends AppCompatActivity implements View.OnClickListener{

    private EditText name;
    private EditText author;
    private EditText iisbn;

    private final String REGISTER_URL = "http://localhost/android/create_book.php";
    private Button Aggiungi;

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

        name = (EditText) findViewById(R.id.nome);
        author= (EditText) findViewById(R.id.autore);
        iisbn = (EditText) findViewById(R.id.isbn);


        Aggiungi = (Button) findViewById(R.id.Aggiungi);
        Aggiungi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == Aggiungi){
            registerUser();
        }
    }

    private void registerUser() {
        String nome = name.getText().toString().trim().toLowerCase();
        String autore = author.getText().toString().trim().toLowerCase();
        String isbn= iisbn.getText().toString().trim().toLowerCase();


        register(nome,autore,isbn);
    }

    private void register(String nome, String autore, String isbn) {
        String urlSuffix = "?nome=nome&autore=autore&isbn=isbn";
        class RegisterUser extends AsyncTask<String, Void, String>{

            ProgressDialog loading;


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(addupdel_activity.this, "Please Wait",null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s, Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {
                String s = params[0];
                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(REGISTER_URL+s);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                    String result;

                    result = bufferedReader.readLine();

                    return result;
                }catch(Exception e){
                    return null;
                }
            }
        }

        RegisterUser ru = new RegisterUser();
        ru.execute(urlSuffix);
    }
}

php file

result

有人可以告诉我,我在哪里弄错了吗?为什么它没有在sql中存储值。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您没有使用传递给register方法的值。改变这一行:

String urlSuffix = "?nome=nome&autore=autore&isbn=isbn";

为:

 String urlSuffix = "?nome="+nome+"&autore="+autore+"&isbn="+isbn";

然后将doInBackground方法更改为:

int responseCode = 0;
try {

        URL url = new URL(REGISTER_URL+s);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");
        responseCode = con.getResponseCode();
        if (responseCode == 200) {
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            in.close();
            return response.toString();
        }

    } catch (Exception ex) {
        Log.i("CaughtException", ex.getMessage());
    }
return "Error Found with Response Code = " + responseCode;
}

答案 1 :(得分:0)

您确定此链接有效吗?

http://localhost/android/create_book.php?nome=nome&autore=autore&isbn=isbn

只是尝试在你的浏览器中触发url,如果它返回一个真实的结果,那么我认为你的doInBackground任务会出现错误。

因为我从不尝试使用ur方法连接db,但我可以建议你使用okHttpClient3库进行数据库连接