Web サーバー上のファイルに関心があるとします。
ここには2つの解決策があります。G: ドライブをネットワーク共有にするか、Windows 用の Apache をインストールし、コードの派生物を使用して Web サーバーから csv ファイルを取得します。
ネットワーク共有ルート:
Android デバイス用の AndSMB などの Samba クライアントが必要です。これは、Windows ボックスから共有ドライブをマウントし、ファイルをデバイスにコピーするために使用されます。次に、他の場合と同じようにファイルを開く必要があります。この Q/A を出発点として使用してください: Android で SD カードから特定のファイルを読み取る
アパッチルート:
Apache をインストールして構成します: http://httpd.apache.org/docs/2.2/platform/windows.html
これは、やりたいことに適応できるはずです。あなたの発信者は、私のコールバックのようなものを実装します。呼び出し元は、AsyncTask を実装するクラスをインスタンス化します。これは、メインの GUI スレッドからネットワーク トラフィックを切り離すために使用されます。そうしないと、3.0(?) 以降で問題が発生します。必要に応じてコードを細かく分割します。これを github のプロジェクトとして立ち上げる必要があります。プロジェクトは、完全なコードが必要な人のためにここにあります: https://github.com/nedwidek/Android-Rest-API
折り返し電話:
package com.hatterassoftware.restapi;
import java.net.URL;
import java.util.HashMap;
import android.os.AsyncTask;
import android.util.Log;
/**
* An AsyncTask implementation for performing POSTs on the Hypothetical REST APIs.
*/
public class GetTask extends AsyncTask<String, String, HttpReturn>{
private String restUrl;
private RestCallback callback;
private HashMap<String, String> headers;
private String username;
private String password;
final String TAG = "GetTask";
/**
* Creates a new instance of PostTask with the specified URL, callback, and
* request body.
*
* @param restUrl The URL for the REST API.
* @param callback The callback to be invoked when the HTTP request
* completes.
* @param requestBody The body of the POST request.
*
*/
public GetTask(String restUrl, RestCallback callback, HashMap<String, String> headers, String username, String password){
this.restUrl = restUrl;
this.callback = callback;
this.headers = headers;
this.username = username;
this.password = password;
}
@Override
protected HttpReturn doInBackground(String... params) {
try {
RestHttpUrlConnection request = new RestHttpUrlConnection(new URL(restUrl), headers, username, password);
return request.doGetRequest();
} catch (Throwable t) {
Log.e(TAG, "Exception in doInBackground", t);
return new HttpReturn(new RestException(t));
}
}
@Override
protected void onPostExecute(HttpReturn result) {
Log.d(TAG, "Entered onPostExecute");
Log.d(TAG, "result.status = " + result.status);
Log.d(TAG, "result.content = " + result.content);
callback.onTaskComplete(result);
super.onPostExecute(result);
}
}
package com.hatterassoftware.restapi;
import java.net.URL;
import java.util.HashMap;
import android.os.AsyncTask;
import android.util.Log;
/**
* An AsyncTask implementation for performing POSTs on the Hypothetical REST APIs.
*/
public class GetTask extends AsyncTask<String, String, HttpReturn>{
private String restUrl;
private RestCallback callback;
private HashMap<String, String> headers;
private String username;
private String password;
final String TAG = "GetTask";
/**
* Creates a new instance of PostTask with the specified URL, callback, and
* request body.
*
* @param restUrl The URL for the REST API.
* @param callback The callback to be invoked when the HTTP request
* completes.
* @param requestBody The body of the POST request.
*
*/
public GetTask(String restUrl, RestCallback callback, HashMap<String, String> headers, String username, String password){
this.restUrl = restUrl;
this.callback = callback;
this.headers = headers;
this.username = username;
this.password = password;
}
@Override
protected HttpReturn doInBackground(String... params) {
try {
RestHttpUrlConnection request = new RestHttpUrlConnection(new URL(restUrl), headers, username, password);
return request.doGetRequest();
} catch (Throwable t) {
Log.e(TAG, "Exception in doInBackground", t);
return new HttpReturn(new RestException(t));
}
}
@Override
protected void onPostExecute(HttpReturn result) {
Log.d(TAG, "Entered onPostExecute");
Log.d(TAG, "result.status = " + result.status);
Log.d(TAG, "result.content = " + result.content);
callback.onTaskComplete(result);
super.onPostExecute(result);
}
}
package com.hatterassoftware.restapi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
import android.util.Base64;
import android.util.Log;
public class RestHttpUrlConnection {
private HttpURLConnection connection;
final String TAG = "RestHttpUrlConnection";
protected RestHttpUrlConnection(URL url, HashMap<String,String> headers, final String username, final String password) throws IOException {
connection = (HttpURLConnection) url.openConnection();
if(headers != null) {
for(String key: headers.keySet()) {
Log.d(TAG, "Adding header: " + key + "; " + headers.get(key));
connection.addRequestProperty(key, headers.get(key));
}
}
if(username != null) {
String auth = "Basic " + Base64.encodeToString((username + ":" + password).getBytes(), Base64.DEFAULT);
connection.addRequestProperty("Authorization", auth);
}
}
public HttpReturn doPostRequest(String postData) throws IOException {
return doPostOrPut(true, postData);
}
public HttpReturn doGetRequest() throws IOException {
return doGetOrDelete(true);
}
public HttpReturn doPutRequest(String putData) throws IOException {
return doPostOrPut(false, putData);
}
public HttpReturn doDeleteRequest() throws IOException {
return doGetOrDelete(false);
}
private HttpReturn doGetOrDelete(boolean isGet) throws ProtocolException, IOException {
if (isGet) {
connection.setRequestMethod("GET");
} else {
connection.setRequestMethod("DELETE");
}
String line;
StringBuffer output = new StringBuffer(1024);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = in.readLine()) != null) output.append(line);
} catch (Exception e) {
HttpReturn httpReturn = new HttpReturn(connection.getResponseCode(), output.toString());
httpReturn.restException = new RestException(e);
return httpReturn;
}
return new HttpReturn(connection.getResponseCode(), output.toString());
}
private HttpReturn doPostOrPut(boolean isPost, String data) throws ProtocolException, IOException {
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setFixedLengthStreamingMode(data.getBytes().length);
if (isPost) {
connection.setRequestMethod("POST");
} else {
connection.setRequestMethod("PUT");
}
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(data);
out.flush();
out.close();
connection.connect();
String line;
StringBuffer output = new StringBuffer(1024);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = in.readLine()) != null) output.append(line);
} catch (Exception e) {
HttpReturn httpReturn = new HttpReturn(connection.getResponseCode(), output.toString());
httpReturn.restException = new RestException(e);
return httpReturn;
}
return new HttpReturn(connection.getResponseCode(), output.toString());
}
}