我有问题。我无法连接到MySQL服务器。我遵循这个教程simplifiedcoding.net/android-php-mysql-login-tutorial-android-login-app-3/,我想做简单的PHP MySQL登录。但是,不推荐使用“NameValuePair”,“BasicNameValuePair”,“HttpClient”,“HttpPost”,“HttpResponse”,“HttpEntity”,“UrlEncodedFormEntity”等内容。我想问,任何人都可以编码这个权利?或者只是帮助我?Android工作室 - http连接处理在Android应用程序
这里是一个不能正常工作的代码: (并感谢您的帮助)
@Override
protected String doInBackground(String... params) {
String uname = params[0];
String pass = params[1];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", uname));
nameValuePairs.add(new BasicNameValuePair("password", pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("site.com/login.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
Apache的HttpClient的是deprecrated并且必须通过HttpURLConnection类来代替。看到这里:http://developer.android.com/reference/java/net/HttpURLConnection.html –
而你的问题标题并没有反映你真正的问题。请改变它。你问的关于在Android应用程序中的http连接处理不是关于AS中的jdbc连接问题。 –
它不帮忙; x – Kahba2000