The thing is, I need to pass a post request to the php file. When transmitted, these requests are assigned to variables ($ name = $ _ GET ['name'];). How to implement this? I tried the h \ s HttpClient, it did not work because the studio does not see this library, tried the h \ s HttpComponent, but displayed errors when assigning the URL to a variable and when creating the HttpURLConnection object. Tell me in what other way you can send this data or how can you follow a link of this type http://site.ru/file.php?name=qwerty at the same time so that nothing happens on the screen?
2 answers
I used the library Retrofit. Here is a tutorial in Russian https://www.youtube.com/watch?v=c5HX_GoyaDs In English, they are much more, and they are more advanced.
|
I found a solution to the issue. First, you need to use AsyncTask, for parallel, background actions, second, here is the code:
public String id = ""; public String name = ""; class MyTask extends AsyncTask<Object, Object, Void> { @Override public Void doInBackground(Object... voids) { try { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(new HttpGet("http://site.ru/get.php/?id=" + id + "&name=" + name)); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); System.out.println(out.toString()); } else{ response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (Exception e) { e.printStackTrace(); } return null; } } |