我有問題。我無法連接到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