package ru.yarteleservice.noc.nocmessages;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Form1 extends AppCompatActivity {
EditText logv;
jsonnoc jsn;
public void AddToLog(String txt) {
logv = (EditText) findViewById(R.id.editText3);
if (txt == "clear") {
logv.setText("");
} else {
logv.append(txt);
logv.append("\n");
}
;
}
;
public void ClickEnterNocButton(View view) {
Log.d("неашипка", "--click!");
AddToLog("clear");
EditText elogin = (EditText) findViewById(R.id.editText);
EditText epass = (EditText) findViewById(R.id.editText2);
AddToLog("-пробуем залогиниться");
// jsonnoc jsn=new jsonnoc();
//jsn.GetJsonMet("{'userlogin':'login'}");
new GetMyDataFromUrl().execute("!!!!!!!!!");
AddToLog("-сохраняем настройки");
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor mEdit = mSettings.edit();
mEdit.putString("userlogin", elogin.getText().toString());
mEdit.commit();
mEdit.apply();
mEdit.putString("userpass", epass.getText().toString());
mEdit.commit();
mEdit.apply();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form1);
AddToLog("-читаем настройки");
EditText elogin = (EditText) findViewById(R.id.editText);
EditText epass = (EditText) findViewById(R.id.editText2);
SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
elogin.setText(mSettings.getString("userlogin", ""));
epass.setText(mSettings.getString("userpass", ""));
AddToLog(elogin.getText().toString());
AddToLog(epass.getText().toString());
}
class GetMyDataFromUrl extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg) {
String res;
BufferedReader reader = null;
String resultJson = "";
res=null;
HttpURLConnection urlConnection = null;
Log.d("неашипка", "--пробую почитать json");
try {
URL url = new URL("http://noc.dfvdfv.ru/getjsonmess.php");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
resultJson = buffer.toString();
Log.d("неашипка", resultJson);
}
catch (Exception e) {
e.printStackTrace();
Log.d("ашипка", "--не смогли прочитать JSON");
}
return res;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
}