Я пытаюсь вызвать и запустить сервлет из моего маленького проекта на Android Studio (v 3.2.1).Проблема в том, что я не могу запустить сервлет, который я вызываю, когда я использовал AsyncTask ...
Я видел десятки уроков и видео, но ничегопохоже, работает, а сервлет даже не запускается, используя метод execute (полный путь сервлета).
public class MainActivity extends AppCompatActivity {
Button goToProfile;
private Intent i;
ProgressBar bar;
STUDENTI u;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = new Intent(getApplicationContext(), profilo.class);
i.putExtra("studente", u);
goToProfile= findViewById(R.id.login);
goToProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// startActivity(i);
ProvaRichiesta r= new ProvaRichiesta();
r.execute("http://localhost:8080/PROGETTO_TWEB/ControllerANDROID");
}
});
}
class ProvaRichiesta extends AsyncTask<String,Void,String> {
HttpURLConnection connection = null;
BufferedReader reader = null;
String result=" ";
String line=" ";
String risp = " ";
EditText user;
EditText password;
String login ="";
@Override
protected void onPreExecute() {
super.onPreExecute();
user = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
bar = (ProgressBar)findViewById(R.id.progressbar);
bar.setVisibility(View.VISIBLE);
goToProfile.setEnabled(false);
if(user != null && user.length()!= 0 && password != null && password.length()!= 0) {
login = "login:" + user.getText() + ":" + password.getText();
}else {
Toast.makeText(getApplicationContext(), "compila tutti i campi", Toast.LENGTH_LONG).show();
}
}
@Override
protected String doInBackground(String... params) {
try {
String richiesta = "?azione=" + login;
URL url = new URL(params[0] + richiesta);
result=makeHTTPrequest(url);
} catch (IOException e) {
System.out.println(e.getMessage());
}
return result;
}
public String makeHTTPrequest(URL url) throws IOException {
String jsonResponse = "";
InputStream inputStream = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
inputStream = connection.getInputStream();
System.out.println("-----------------------------------"+inputStream);
jsonResponse = readFromStream(inputStream);
} catch (IOException e) {
} finally {
if (connection != null) {
connection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
private String readFromStream(InputStream inputStream)throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader((inputStreamReader));
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSONObject obj;
JSONArray array;
bar.setVisibility(View.INVISIBLE);
goToProfile.setEnabled(true);
try {
array = new JSONArray(result);
risp = array.getJSONObject(0).getString("risposta");
if (risp.equals("no")) {
Toast.makeText(getApplicationContext(), "username o password scorretti", Toast.LENGTH_SHORT).show();
} else {
obj = array.getJSONObject(1);
u.setUSERNAME(obj.getString("nomeUtente"));
u.setSTUDENTE_PPW(obj.getString("password"));
u.setRUOLO(obj.getString("ruolo"));
startActivity(i);
}
} catch (JSONException e) {
System.out.println(e.getMessage());
}
}
}
}