2
I started learning Android programming very little time ago. I was programming in Java desktop but I never made any application to communicate with a server.
Now I need to communicate with a server through the method post
in an Android application. I was able to do this, but I can’t send parameters to the server.
The server must register and send a reply and it should receive an image and some other data and I already searched and I can’t do it.
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 the HttpClient
and neither is this NameValuePair
and the HttpUrlConnection
does not have the method setEntity
. That’s the problem. The internet only talks about this HttpClient
and it’s not right for me. I’m a beginner and I can’t continue and I have time to finish . just send the parameters to the server! only this!