Reputation: 653
I added this method to my MainActivity.java But i'm getting error on the static: Inner classes cannot have static declatarions.
protected static String excutePost(String targetURL, String urlParameters) {
HttpURLConnection connection = null;
try {
//Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();
//Get Response
java.io.InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new java.io.InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if not Java 5+
String line;
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
I can remove the static and then i'm getting no errors. But where do i call this method from now ? From inside the onCreate ? Tried but it's not exist when i type excute....in the onCreate it's not exist can't call it.
How do i use this method ? And what should i put as urlParameters ?
Upvotes: 1
Views: 93
Reputation: 1629
The outer class which holds the "excutePost" is an inner class therefore it cannot have static methods in it. What you can do is move the outer class to a separate file and make it non static - this will solve your problem.
Upvotes: 1