2017-02-20 63 views
-1

我目前正在研究一個需要從在線mySQL數據庫下載所有內容的android應用程序,所有這些應用程序都將使用內置於android的SQLite本地存儲。連接到mysql服務器的Android設備

我現在面臨的問題是從android設備連接到服務器。我還沒有想出如何拉取信息。

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

import org.apache.http.NameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import static android.content.ContentValues.TAG; 

public class LoginWorker { 

JSONParser jParser = new JSONParser(); 

ArrayList<HashMap<String, String>> usersList; 

private static String url_all_users = "http://localhost/get_user_login.php"; 

private static String TAG_SUCCESS = "success"; 
private static String TAG_USERS = "users"; 
private static String TAG_EMAIL = "email"; 
private static String TAG_PASSWORD = "password"; 

JSONArray users = null; 

public void onCreate(){ 
    System.out.print("Login worker onCreate started"); 
    usersList = new ArrayList<HashMap<String, String>>(); 

    HashMap<String, String> map = new HashMap<>(); 
    map.put("test","test"); 

    usersList.add(map); 

    Log.d(TAG, "onCreate: LoadAllUsers Running now"); 

    new LoadAllUsers().execute(); 

} 

class LoadAllUsers extends AsyncTask<String, String, String>{ 

    protected void onPreExecute(){ 
     super.onPreExecute(); 
    } 

    protected String doInBackground(String... args){ 

     Log.d(TAG, "doInBackground: Start of method"); 

     List<NameValuePair> params = new ArrayList<NameValuePair>(); 

     JSONObject json = jParser.makeHttpRequest(url_all_users, "GET", params); 



     try { 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       users = json.getJSONArray(TAG_USERS); 

       for (int i = 0; i < users.length(); i++) { 
        JSONObject c = users.getJSONObject(i); 

        String password = c.getString(TAG_PASSWORD); 
        String email = c.getString(TAG_EMAIL); 

        HashMap<String, String> map = new HashMap<String, String>(); 

        map.put(TAG_EMAIL, email); 
        map.put(TAG_PASSWORD, password); 

        usersList.add(map); 
       } 
      } else { 
       Log.d("doInBackground : ", "No user found"); 
      } 
     }catch(JSONException e){ 
       e.printStackTrace(); 
      } 
     return null; 
     } 

    protected void onPostExecution(){} 
} 

這是我一直在處理的一些代碼(用於用戶登錄)的示例。有沒有一種更簡單的方式來使這種連接,而不必使用JSON和PHP?任何幫助將不勝感激,即使它只是鏈接到有用的博客或教程!

回答

相關問題