I have an Android application that should request a JSON to a web application, however to access the method it is necessary to log in to the site. How do I perform this identification via code?
Webservice.java
package br.ufscar.dc.controledepatrimonio.Util.Webservice;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class Webservice {
private URL url;
private HttpURLConnection con = null;
public Webservice(String url) {
try {
this.url = new URL(url);
con = (HttpURLConnection) this.url.openConnection();
} catch (MalformedURLException ex) {
Log.d("MalformedURLException", ex.getMessage());
} catch (IOException ex) {
Log.d("IOException", ex.getMessage());
}
}
public String getJSON() {
try {
con.setRequestMethod("GET");
con.setRequestProperty("Content-length", "0");
con.setUseCaches(false);
con.setAllowUserInteraction(false);
con.connect();
int status = con.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
}
} catch (ProtocolException ex) {
Log.d("ProtocolException", ex.getMessage());
} catch (IOException ex) {
Log.d("IOException", ex.getMessage());
} finally {
if (con != null) {
try {
con.disconnect();
} catch (Exception ex) {
Log.d("Exception", ex.getMessage());
}
}
}
return null;
}
}
LocalTask.java
package br.ufscar.dc.controledepatrimonio.Util.Webservice;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
public class LocalTask extends AsyncTask<Void, Void, String> {
private Context ctx;
private ITask iTask;
private String retorno = null;
public LocalTask(Context ctx, ITask iTask) {
this.ctx = ctx;
this.iTask = iTask;
}
@Override
protected String doInBackground(Void... params) {
Webservice webservice = new Webservice("http://192.168.0.10:8080/Patrimonio/local/index.json");
retorno = webservice.getJSON();
return retorno;
}
@Override
protected void onPostExecute(String s) {
iTask.getJSON(retorno);
}
}
When executing the command retorno = webservice.getJSON();
the return that I have is the HTML of the login page, not JSON.