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

问题描述:

我想从我的android应用程序获取数据并将其放入sql数据库中,该数据库位于phpmyadmin(wampp服务器)中。 这是我的代码:从android应用程序获取数据,并将其存储在sql数据库中使用php

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中存储值。

在此先感谢。

您没有使用传递给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; 
} 
+0

这是给我这个错误:错误:无法找到符号变量responseCode – Sara

+0

之前刚刚intialize它'try' block as int'responseCode = 0'; –

+0

doinbackground中缺少return语句。 – Sara

你确保这个环节的工作?

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

只是尝试火网址在浏览器乌尔,如果它的返回一个真实结果,那么我认为错误是发生在乌尔doInBackground任务。

,因为我从来没有尝试使用乌尔方法来连接数据库,但我可以建议ü使用okHttpClient3库数据库连接,而不是

+0

我改变了链接,它正在工作。 – Sara