Reputation: 55
I have a problem! I have a connection with a php page to connect with database and the method works fine on Froyo, but doesn't work on newer versions of Android. What is the problem?
public static String interact(String request){
String result="test";
String site = "http://xxxx.net:nnn/xxx";
URL url = new URL(site);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.connect();
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(request);
wr.flush();
wr.close();
Scanner in = new Scanner(connection.getInputStream());
while(in.hasNextLine())
result+=in.nextLine();
connection.disconnect();
return result;
}
catch (UnknownHostException e) {
return e.getMessage();
} catch (IOException ex) {
return ex.getMessage();
}
catch (Exception ex){
return ex.getMessage();
}
}
I need to send data and get response, but it just gives me null on newer Android than 2.2.
Upvotes: 1
Views: 138
Reputation: 51
Yes this is not allowed on version lower than 3.0. however you can override the rules by doing some setting bypass but at the very least don't do that as it might give app-crash to you.
I suggest to raise the level of your min-SDK level.
Upvotes: 0
Reputation: 12642
you are running Network Request
on main thread.
Android >=3.0 does not alow this. you need to use AsyncTask to call Network Request
Upvotes: 4