I am new to Android development. I am trying to join an existing VPN from my Android app. I want to integrate the VPN in my app; my app then is supposed to query a remote database.
I got some code and tried to use it to create a VPN. It emulates the built-in VPN manager on the Android phone. The code compiles and the manager is launched but the connection to the VPN does not succeed when I try to connect after all configuration. The protocol is PPTP. A VPN exists and has been tested.
I tried connecting from an android phone with the same settings and it was successful.
I thought maybe I am passing the parameters in the wrong way. I have put the code for the vpn part below. The url is not the actual but in same format.
Any help to identify what I am doing wr开发者_高级运维ong would be appreciated. Also if there is a way I can directly call the VPN manager from my app.
Thanks very much for any help,
final Button button1 = (Button)findViewById(R.id.button1);
final Button button2 = (Button)findViewById(R.id.button2);
final Button button3 = (Button)findViewById(R.id.button3);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("android.net.vpn.SETTINGS"));
}
});
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
URL url = null;
try {
String registrationUrl = String.format("daffy.zune.org");
url = new URL(registrationUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Log.d("MyApp", "Registration success");
} else {
Log.w("MyApp", "Registration failed for: " + registrationUrl);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.register);
}
});
}
}
I suggest you look at the simplevpn project and browse the source here and view ShowAllVPNsActivity.java.
Hope this helps! We need more good VPN apps in the Market!
精彩评论