2017-08-14 76 views
0

调用方法与POJO在服务器上(JAVA)机器人如何在服务器

public class DbAccessor { 

    public Employee getEmployee(Long id) { 
     // fetch from database 
    } 

    public List<Employee> getEmployees(Criteria criteria) { 
     // fetch from database 
    } 

    public void createEmployee(Employee e) { 
     // persist 
    } 
} 

员工和标准是POJO。

如何从android设备调用方法?如何把它变成网络服务? Json还是XML? REST还是SOAP?请指教。

回答

0

假设您已经获得某种数据库的员工数据||标准存储。从Android内部访问SQL数据库可能是可能的,但我会假设你需要一些用于数据库的驱动程序。我的选择是调用访问数据库的服务器端脚本,并将请求的表格作为JSON返回。

Android的呼叫服务器异步与排球请求

假设你正在使用GSON和截击在Android的 编译 'com.google.code.gson:GSON:2.8.0' 编译“com.android.volley :凌空:1.0.0'

import android.os.AsyncTask; 
import android.util.Log; 

import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.JsonObjectRequest; 
import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 

import org.json.JSONObject; 

/** 
* Created by Henrik Ekelin on 08/14/2017. 
*/ 
public class ServerRequest extends AsyncTask<Void, Void, String> { 

    private final static String TAG = ServerRequest.class.getSimpleName(); 
    private final static String API_URL = "URL/fetchJSON.php"; 

    private ServerMsgCallback callback; 

    public interface ServerMsgCallback { 
     void onRetrieved(Employee model); 

     void onError(String error); 
    } 

    public gay(ServerMsgCallback callback) { 
     this.callback = callback; 
     execute(); 

    } 

    @Override 
    protected String doInBackground(Void... voids) { 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(API_URL, null, new Response.Listener<JSONObject>() { 

      @Override 
      public void onResponse(JSONObject response) { 
       Log.d(TAG, "onResponse() " + response.toString()); 

       Gson gson = new GsonBuilder().create(); 
       // Retrieve the fetched JSON as Employee POJO 
       Employee employee = gson.fromJson(response.toString(), Employee .class) 
       callback.onRetrieved(gson.fromJson(response.toString(), Employee.class)); 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "onErrorResponse() " + error.getMessage()); 
       callback.onError(error.getMessage()); 

      } 
     }); 

     // Add the JSONObject to App Volley RequestQuew 
     App.getInstance().addToRequestQueue(jsonObjectRequest); 

     return null; 
    } 

} 

PHP脚本连接到SQL Server

$db = null; 
// Connect to database. 
try { 
    $db = new pdo ('mysql:host=127.0.0.1:3306;dbname=DATABASE_NAME', 'USER', 'PASSWORD'); 
} catch (PDOException $ex) { 
    // Die with mercy 
    die (json_encode(array(
     'outcome' => false, 
     'message' => 'Unable to connect' 
    ))); 
} 

if ($db != null) { 

    // Retrieve the _POST msg from client as array 
    // {key -> value] 
    if (array_key_exists('msg', $_POST)) { 
     $data = json_decode($_POST ['msg'], true); 

     // Set the value from key 'id' 
     $id = $data ['id']; 

     // SQL statement retrieving the correct table with ID -> $id 
     // Prepare SQL statement 
     $s = $db->prepare('SELECT * FROM Employee WHERE ID = :id'); 
     // Bind the parameter :name 
     $s->bindParam(':id', $id, PDO::PARAM_STR); 
     // Execute 
     $s->execute(); 
     // Fetch the result 
     $r = $s->fetchAll(); 

     // Loop through the result 
     foreach ($r as $res) { 

      // Find the correct Emplyee with id 
      if ($res ['ID'] == $id) { 

       // Retrieve the table params and store them as an array 
       $arr = array('ID', $res['ID'], 'NAME', $res['NAME'], 'SOMETHING', $res['SOMETHING'], 'BADABING', $res['BADABING']); 

        // Echo the result ie: return JSON 
       echo json_encode(array("Employee" => $arr)); 
      } 
     } 

    } 

} 

$db = null; 
header('Name: ' . "/location/file.php"); 

祝您好运!

+0

在服务器上它说** Java **,但你还是添加了php脚本。 – androidnoobdev

+0

没关系,只需在服务器上放一个fetchJSON.php文件,然后从Android中调用它即可!内容从调用返回 – gekn76

+0

Java服务器如何运行php文件?你有一些知识或只是想获得投票的答案? – androidnoobdev