📜 ⬆️ ⬇️

Programming on Android for a web developer or a quick start for the little ones. Part 2

Greetings

The article is a continuation of part 1 I started.

Warning


Important : this lesson is not professional. The author of the lesson is not an expert in programming for the Android platform. I apologize in advance for unwarranted expectations.
')

RegistrationActivity


In Activity you need to do the following to register:

Reading the form elementary and complications should not:

final EditText login = (EditText)findViewById(R.id.login); final EditText password = (EditText)findViewById(R.id.password); final EditText password2 = (EditText)findViewById(R.id.password2); //     


Works with the network in the new stream


Using AsyncTask, create a background thread:

 // 3       doInBackground,      onPostExecute class AsyncTaskExample extends AsyncTask<Void, Integer, String> { //   @Override protected String doInBackground(Void... params) { } //   doInBackground,    UI protected void onPostExecute(String result) { } catch (JSONException e) { e.printStackTrace(); } } } //   new AsyncTaskExample().execute(); 


I rendered requests to the server into a separate class (classes are located in the same pack as the Activity). The class method accepts the login and password and returns the server response. It might look something like this:

 public class ServerSendData { private static String server = "http://xxx.xxx.x.xxx/"; public static String mlogin = null; public static String mpassword = null; public static String sendRegData(String login,String password) { String result = null; mlogin = login; mpassword = password; try { URL url = new URL("" + server + "apiregistration/create/"+mlogin+"/"+mpassword+""); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream in = httpConnection.getInputStream(); BufferedReader r = new BufferedReader(new InputStreamReader(in)); result = r.readLine(); Log.w("res",""+result+""); } else { } } catch (MalformedURLException e) {} catch (IOException e1) {} return result; } 

It should be noted that my server communicates in JSON. The answer is:

 {"status":"login_busy"} 

Next, the matter of technology. We refer to the class in the doInBackground method:

 return ServerSendData.sendRegData(""+login.getText().toString()+"",""+ password.getText().toString()+""); 

In onPostExecute we work with the result:

 JSONObject object = new JSONObject(result); String status = object.getString("status"); // status   login_busy 


To save my time and your nerve cells, I dare to attach a survey.

Source: https://habr.com/ru/post/165251/


All Articles