2015-07-12 96 views
1

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

Apache的HttpClient的是deprecrated並且必須通過HttpURLConnection類來代替。看到這裏:http://developer.android.com/reference/java/net/HttpURLConnection.html –

+0

而你的問題標題並沒有反映你真正的問題。請改變它。你問的關於在Android應用程序中的http連接處理不是關於AS中的jdbc連接問題。 –

+0

它不幫忙; x – Kahba2000

回答

0

其實你發佈的鏈接是從我的網站。當我編寫本教程時,這些類不會被棄用。如果你會檢查我的網站,那麼我已經發布了一個更新的教程。

要發送一個http post請求到你的web服務器,你可以使用下面的代碼。

public class RequestHandler { 
public String sendPostRequest(String requestURL, 
           HashMap<String, String> postDataParams) { 
    URL url; 

    StringBuilder sb = new StringBuilder(); 
    try { 
     url = new URL(requestURL); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     conn.setReadTimeout(15000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 

     OutputStream os = conn.getOutputStream(); 

     BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8")); 
     writer.write(getPostDataString(postDataParams)); 

     writer.flush(); 
     writer.close(); 
     os.close(); 
     int responseCode = conn.getResponseCode(); 

     if (responseCode == HttpsURLConnection.HTTP_OK) { 

      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      sb = new StringBuilder(); 
      String response; 
      while ((response = br.readLine()) != null){ 
       sb.append(response); 
      } 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return sb.toString(); 
} 

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException { 
    StringBuilder result = new StringBuilder(); 
    boolean first = true; 
    for (Map.Entry<String, String> entry : params.entrySet()) { 
     if (first) 
      first = false; 
     else 
      result.append("&"); 

     result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); 
    } 

    return result.toString(); 
} 
} 

現在,所有你需要做的就是在你裏面主要活動創建的AsyncTask和doInBackground方法中

調用sendPostRequest()方法

你必須通過你的網址腳本來處理髮布請求和散列表中的參數以及請求發送數據。

欲瞭解更多詳情,請訪問Android PHP MySQL - CRUD Operation

相關問題