I'm new to android and LAMP.
I have some data on the MySQL server, and I'm trying to write an app in order to access that data.
Meanwhile, all I want to do is to press a button and get specific information (the URL is hard-coded).
When I type in the url in chrome (running on my android phone) I get exactly what I'm looking for, but when I try getting the same info through the app, the app crashes. this is the code:
public void getInfo(View view) throws Exception {
try {
URL url = new URL("http://192.168.1.57:80/TOTSphp/getAllUsersInfo.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", "TOTS");
//int responseCode = conn.getResponseCode();
BufferedReader in;
conn.setDoOutput(true);
conn.setDoInput(true);
try {
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
in.close();
}
} finally {
conn.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
The code appears right after the onCreate()
method and is called through a button with android:onClick="getInfo"
.
While trying to find an answer, I added:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
to AndroidManifest.xml
, but that didn't seem to work....
Thanks a lot!!!!