Connecting an android phone to a server over http and outputstream - android

I am trying to connect an android phone to a server running on my local machine over http and outputstream the content but unsuccessful..!
here is my code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
URL url = new URL("http://192.168.24.42:80/DebugServer/");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
private void readStream(InputStream in) {
try {
in.read();
in.read();
} catch (IOException e) {
e.printStackTrace();
}
}

URL url = new URL("http://192.168.24.42:80/DebugServer/");
is your connection URL. Change the port to 8080 because that is where the server listens to.
Also, did you go to the bin folder of your tomcat (I am assuming that here) and run the startup.BAT or startup.SH file because without that your server is not running.
To check if your server is properly running, try http://localhost:8080
And please, post the logcat :)

Related

How to get XML data from https url in Android

I want to get XML data from the web server https://ruralfire.qld.gov.au/bushfirealert/bushfireAlert.xml
However, I can't do it because my codes always have an error "javax.net.ssl.SSLHandshakeException: Connection closed by peer".
And my InputStream is always null, so I can't do anything with it (such as parsing).
I ensure that the problem is in my connection but I don't know how to solve it.
This is my code in connecting to the web server:
private InputStream downloadUrl(String urlString) throws IOException {
URL url;
url = new URL(urlString);
InputStream is = null;
try {
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setDoInput(true);
is = con.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
May you help me to solve this problem ?
Thank you so much.

How to Connect Android Device with web serve using AsyncTask

I have this asynctask.I need to connect device with web server.Need to send a JSON Arry and receive JSON array. Can i use httpUrlConnection ? or httpClient. Does httpClient support latest versions of Android?
class background_thread extends AsyncTask<JSONArray, Void, Boolean> {
protected Boolean doInBackground(JSONArray... params) {
//connect with server side php script
String UR = "127.0.0.1/abc/index.php";
try {
URL url = new URL(UR);
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
json_array=json_encode();
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
writeStream(out);
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
You should use google's own volley library to make network calls and handle the response data in model classes. This is the most modular approach. Refer this link for official documentation.

Android: Quick web requests

For my application I need to have the latest data from an webpage that is hosted on a server on my local network.
So I request the latest page with a HTTP GET and when the data is received, I send another request.
With my current implementation I reach around the 100 - 120 ms per request. Is there a possibility to make this quicker because it's the same url that is requested.
For example keep the connection open to the page and grep the latest data without setting up a new connection?
This page is around the 900-1100 bytes.
HTTP get code:
public static String makeHttpGetRequest(String stringUrl) {
try {
URL url = new URL(stringUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(300);
con.setConnectTimeout(300);
con.setDoOutput(false);
con.setDoInput(true);
con.setChunkedStreamingMode(0);
con.setRequestMethod("GET");
return readStream(con.getInputStream());
} catch (IOException e) {
Log.e(TAG, "IOException when setting up connection: " + e.getMessage());
}
return null;
}
Reading inputstream
private static String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder total = new StringBuilder();
try {
String line = "";
reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException when reading InputStream: " + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return total.toString();
}
As I know there isn't an implementation like you are asking for. I've been dealing a lot with http requests and the best thing you can do is your code. There is another thing which need some attention...your connection maybe slow and depending on that connection time can be more or in some cases which I've been dealing a lot the connection's timeout isn't enough big, but that's server problem.
In my opinion you should use what you have now.

Can't connect to local area server with asp.net page on Android

I'm new here and I wish I won't make any bloomer :)
I'm developing some android app and have stucked in connecting to other computer, where I've installed ASP.net page. I can't emulate it due to problems with connecting to host.
The code that I'm using:
public class SPCChartViewerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String strData = urlData("http://[server]/SPCOnline/SQLQuery.aspx?query=SELECT%20NAME%20FROM%20SPC_CHARTS");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(strData);
}
private String urlData(String urlString){
URLConnection urlConnection = null;
URL url = null;
String string = null;
try {
url = new URL(urlString);
urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
while((string = reader.readLine()) != null){
stringBuffer.append(string + "\n");
}
inputStream.close();
string = stringBuffer.toString();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return string;
}
}
In tag [server] I've put:
that computer's name: http://miltstx155srv01/SPCOnline/SQLQuery.aspx?query=SELECT%20NAME%20FROM%20SPC_CHARTS
In this case I receive java.net:UnknownHostException
that computer's IP: http://10.72.152.163/SPCOnline/SQLQuery.aspx?query=SELECT%20NAME%20FROM%20SPC_CHARTS
In this case I receive java.net:FileNotFoundException
If I ommit the "http://" I receive java.net:Protocol Not Found
Ok, maybe something with my company's DNS. So I've put this page on my computer, which is also running AVD emulator. So the url is now: http://10.0.2.2/SPCOnline/SQLQuery.aspx?query=SELECT%20NAME%20FROM%20SPC_CHARTS - but still java.net:UnknownHostException.
Of course clause uses-permission android:name="android.permission.INTERNET" is added to manifest.
Still, every example written upper works fine with webbrowser (of course except 10.0.2.2 - but changing it to localhost makes it work).
No idea, I've spent whole day testing it. Can anyone help?

how to know file exist on server or not in android

I want to know if a specific file exists a on server or not. For example, suppose I have an .xml file on my server and I want to know if the file is there or not through java from my android application.
What server is it? If its HTTP server, you can request that file and check from response if it exist. If its custom server you have to implement that feature yourself.
You could use something like this:
URL url = null;
URLConnection connection = null;
InputStreamReader stream = null;
// Parse the URL
try {
url = new URL(urlString);
} catch (MalformedURLException e1) {
System.out.println("Malformed URL");
return null;
}
// Open a connection to the URL
try {
connection = url.openConnection();
} catch (IOException e) {
System.out.println("Could not create connection");
return null;
}
// Get the remote file
try {
stream = new InputStreamReader(connection.getInputStream());
} catch (IOException e) {
System.out.printf("File \"%s\" does not exist!",urlString);
return null;
}

Categories

Resources