将 MYSQL 用于我的 android 应用程序:
主要活动。
public class MainActivity extends Activity implements OnClickListener {
Button login;
EditText username, password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup();
}
private void setup() {
// TODO Auto-generated method stub
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
status = (TextView) findViewById(R.id.status);
login.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.login:
login();
break;
}
}
private void login() {
// TODO Auto-generated method stub
try {
httpclient= new DefaultHttpClient();
httppost= new HttpPost("http://109.74.245.109/~profiled/android/index.php");
// Adding Data:
nameValuePair = new ArrayList<NameValuePair>(1);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePair.add(new BasicNameValuePair("username",username.getText().toString().trim()));
nameValuePair.add(new BasicNameValuePair("password",password.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
status.setText(""+response);
if(response.equalsIgnoreCase("No user found")) {
startActivity(new Intent(MainActivity.this, UserPage.class));
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
这是我的错误:
05-17 13:39:25.309: W/System.err(292): java.net.SocketException: Permission denied
05-17 13:39:25.318: W/System.err(292): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
05-17 13:39:25.318: W/System.err(292): at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
05-17 13:39:25.318: W/System.err(292): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
05-17 13:39:25.318: W/System.err(292): at java.net.Socket.checkClosedAndCreate(Socket.java:873)
05-17 13:39:25.318: W/System.err(292): at java.net.Socket.connect(Socket.java:1020)
05-17 13:39:25.333: W/System.err(292): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-17 13:39:25.337: W/System.err(292): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-17 13:39:25.337: W/System.err(292): at com.profiledt.helloworld.MainActivity.login(MainActivity.java:88)
05-17 13:39:25.337: W/System.err(292): at com.profiledt.helloworld.MainActivity.onClick(MainActivity.java:70)
05-17 13:39:25.337: W/System.err(292): at android.view.View.performClick(View.java:2408)
05-17 13:39:25.337: W/System.err(292): at android.view.View$PerformClick.run(View.java:8816)
05-17 13:39:25.337: W/System.err(292): at android.os.Handler.handleCallback(Handler.java:587)
05-17 13:39:25.337: W/System.err(292): at android.os.Handler.dispatchMessage(Handler.java:92)
05-17 13:39:25.357: W/System.err(292): at android.os.Looper.loop(Looper.java:123)
05-17 13:39:25.357: W/System.err(292): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-17 13:39:25.357: W/System.err(292): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 13:39:25.357: W/System.err(292): at java.lang.reflect.Method.invoke(Method.java:521)
05-17 13:39:25.357: W/System.err(292): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-17 13:39:25.357: W/System.err(292): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-17 13:39:25.357: W/System.err(292): at dalvik.system.NativeStart.main(Native Method)
我不完全理解这个错误,但由于它是权限,我得出了一些结论:
- 无法连接到mysql数据库
- 与IP地址和我的服务器有关吗?