简体   繁体   English

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

[英]get data from android app and store it in sql database using php

I want to get data from my android app and put in sql database,which is in phpmyadmin(wampp server).我想从我的 android 应用程序中获取数据并放入 sql 数据库中,该数据库位于 phpmyadmin(wampp 服务器)中。 this is my code:这是我的代码:

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 php文件

result :结果

Can someone please tell me where Am I making mistake?有人可以告诉我我哪里出错了吗? why it is not storing values in sql.为什么它不在 sql 中存储值。

Thanks in advance.提前致谢。

You are not using the values passed into the register method.您没有使用传递给register方法的值。 Change this line:更改此行:

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

to:到:

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

And then change the doInBackground method to:然后将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;
}

do you make sure that this link is working ?你确定这个链接有效吗?

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

just try to fire the url in ur browser, if its return a true result, then i think the mistakes is occur on ur doInBackground task.只是尝试在您的浏览器中触发 url,如果它返回一个真实的结果,那么我认为错误发生在您的 doInBackground 任务上。

since i never try to connect db using ur method, but i can suggest u to use okHttpClient3 library for db connection instead因为我从不尝试使用你的方法连接数据库,但我可以建议你使用 okHttpClient3 库来连接数据库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM