Não conseguido inserir dados no banco de dados, enviando dados do aplicativo android para o banco de dados phpmyadmin.
public class MainActivity extends AppCompatActivity implements Response.Listener<JSONObject>, Response.ErrorListener { TextView CampoId; Button Botao; EditText CampoNome; ProgressDialog BarraProgresso; RequestQueue Requisicao; JsonObjectRequest JsonObjetoRequisicao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CampoId = findViewById(R.id.CampoId); CampoNome = findViewById(R.id.CampoNomeId); Botao = findViewById(R.id.BtnID); Requisicao = Volley.newRequestQueue(getApplicationContext()); Botao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { carregarWebService(); } }); } private void carregarWebService() { String url = "http://127.0.0.1/androidbd/registro.php?codigo="+ CampoId.getText().toString() + "&nome="+ CampoNome.getText().toString(); url = url.replace("", "%20"); JsonObjetoRequisicao = new JsonObjectRequest(Request.Method.GET, url, null,this, this); Requisicao.add(JsonObjetoRequisicao); } @Override public void onErrorResponse(VolleyError error) { BarraProgresso.hide(); Toast.makeText(getApplicationContext(),"Não conectado", Toast.LENGTH_LONG).show(); } @Override public void onResponse(JSONObject response) { BarraProgresso.hide(); Toast.makeText(getApplicationContext(),"Código de cadastro deu certo!", Toast.LENGTH_LONG).show(); CampoId.setText(""); CampoNome.setText(""); } }
Arquivo – registro.php
<?php include "conexao.php"; $ json = array(); if(isset($ _GET["nome"])){ $ codigo = $ _GET["id"]; $ nome = $ _GET["nome"]; $ inserir = "INSERT INTO testeconexao (id, nome) VALUES ('{$ codigo}', '{$ nome}')"; $ resultado_inserir = mysqli_query($ conexao, $ inserir); if($ resultado_inserir){ $ consulta = "SELECT * FROM testeconexao WHERE id = '{$ codigo}' "; $ resultado = mysqli_query($ conexao, $ consulta); if($ registro= ($ resultado)){ $ json['curso'][] = $ registro; } mysqli_close($ conexao); echo json_encode($ json); echo $ _GET['id'] . " " . $ _GET['nome']; }else{ $ result["codigo"] = 0; $ result["nome"] = 'Não Registrado'; $ json['curso'][] = $ result; echo json_encode($ json); } }else{ $ result["codigo"] = 0; $ result["nome"] = 'Não Registrado'; $ json['curso'][] = $ result; echo json_encode($ json); } ?>
apresenta erro no Android Studio:
08/03 00:08:47: Launching app $ adb install-multiple -r -t -p com.example.conexaobdws /home/alanpn/AndroidStudioProjects/ConexaoBDWS/app/build/intermediates/split-apk/debug/slices/slice_0.apk Split APKs installed in 761 ms $ adb shell am start -n “com.example.conexaobdws/com.example.conexaobdws.MainActivity” -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Client not ready yet..Waiting for process to come online Connected to process 8927 on device Nexus_5X_API_29_x86 [emulator-5554] Capturing and displaying logcat messages from application. This behavior can be disabled in the “Logcat output” section of the “Debugger” settings page. W/ple.conexaobdw: JIT profile information will not be recorded: profile file does not exits. I/chatty: uid=10134(com.example.conexaobdws) identical 10 lines W/ple.conexaobdw: JIT profile information will not be recorded: profile file does not exits. I/InstantRun: starting instant run server: is main process W/ple.conexaobdw: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed) Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed) D/: HostConnection::get() New Host Connection established 0xebc3df40, tid 9573 D/: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_host_composition_v1 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 D/OpenGLRenderer: Swap behavior 1 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without… D/OpenGLRenderer: Swap behavior 0 D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0 D/EGL_emulation: eglCreateContext: 0xebb82360: maj 2 min 0 rcv 2 D/EGL_emulation: eglMakeCurrent: 0xebb82360: ver 2 0 (tinfo 0xdf27a790) W/Gralloc3: mapper 3.x is not supported D/: createUnique: call HostConnection::get() New Host Connection established 0xebc3f700, tid 9573 D/: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_host_composition_v1 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 D/OpenGLRenderer: Setting buffer count to 3, min_undequeued 1, extraBuffers 0 D/EGL_emulation: eglMakeCurrent: 0xebb82360: ver 2 0 (tinfo 0xdf27a790) D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0 I/AssistStructure: Flattened final assist data: 1900 bytes, containing 1 windows, 11 views E/Volley: [308] NetworkDispatcher.processRequest: Unhandled exception java.lang.RuntimeException: Bad URL %20%h%20%t%20%t%20%p%20%:%20%/%20%/%20%1%20%2%20%7%20%.%20%0%20%.%20%0%20%.%20%1%20%/%20%a%20%n%20%d%20%r%20%o%20%i%20%d%20%b%20%d%20%/%20%r%20%e%20%g%20%i%20%s%20%t%20%r%20%o%20%.%20%p%20%h%20%p%20%?%20%c%20%o%20%d%20%i%20%g%20%o%20%=%20%e%20%w%20%e%20%&%20%n%20%o%20%m%20%e%20%=%20%1%20%1%20% java.lang.RuntimeException: Bad URL %20%h%20%t%20%t%20%p%20%:%20%/%20%/%20%1%20%2%20%7%20%.%20%0%20%.%20%0%20%.%20%1%20%/%20%a%20%n%20%d%20%r%20%o%20%i%20%d%20%b%20%d%20%/%20%r%20%e%20%g%20%i%20%s%20%t%20%r%20%o%20%.%20%p%20%h%20%p%20%?%20%c%20%o%20%d%20%i%20%g%20%o%20%=%20%e%20%w%20%e%20%&%20%n%20%o%20%m%20%e%20%=%20%1%20%1%20% at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:171) at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87) Caused by: java.net.MalformedURLException: no protocol: %20%h%20%t%20%t%20%p%20%:%20%/%20%/%20%1%20%2%20%7%20%.%20%0%20%.%20%0%20%.%20%1%20%/%20%a%20%n%20%d%20%r%20%o%20%i%20%d%20%b%20%d%20%/%20%r%20%e%20%g%20%i%20%s%20%t%20%r%20%o%20%.%20%p%20%h%20%p%20%?%20%c%20%o%20%d%20%i%20%g%20%o%20%=%20%e%20%w%20%e%20%&%20%n%20%o%20%m%20%e%20%=%20%1%20%1%20% at java.net.URL.(URL.java:601) at java.net.URL.(URL.java:498) at java.net.URL.(URL.java:447) at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:92) at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:131) at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:120) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:87)