I started learning Android programming very fast. I was programming in Java desktop but never made any application to communicate with a server.
Now I need to communicate with a server via the post
method in an Android application. I have managed to do this, however I can not send parameters to the server.
The server must register and send a response and it should receive an image and some other data and I already researched and can not do that.
Follow my code so far:
//Converte um InputStream em String
private String readIt(InputStream stream, int len) throws Exception {
try {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}catch(Exception e)
{
throw e;
}
}
//O código propriamente dito que se comunicará com o servidor
private String downloadUrl(String myUrl) throws Exception {
InputStream is = null;
String respStr = null;
int len = 500;
try {
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.connect();
is = conn.getInputStream();
respStr = readIt(is, len);
} catch(Exception e)
{
throw e;
} finally {
if (is != null) {
is.close();
}
}
return respStr;
}
}
My Android Studio does not recognize HttpClient
and neither NameValuePair
nor HttpUrlConnection
does not have the setEntity
method. That's the problem. The internet only talks about this HttpClient
and it does not work for me. I'm a beginner and I can not continue and I have time to finish. just send the parameters to the server! just this!