我正在开发Android应用程序,它将在列表视图中显示任何文件托管服务上可用的文件,以便我可以下载它们。如果对我有用,我可以使用任何文件托管服务。我试过dropbox但不适合我。请给我一些关于这个主题的代码或任何内容。我甚至尝试过Apache服务,但没有工作。 谢谢!!!
package com.example.mangesh.comp;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ass_bce extends ActionBarActivity {
DLFiles clientServerInterface = new DLFiles();
private String localjsonString="{\"data\":[{\"file_name\": \"file.pdf\", \"physical_path\": \"/pic/file.png\"}]";
private ListView listView;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ass_bce);
listView = (ListView) findViewById(R.id.listView1);
new RetreiveData().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_ass_bce, menu);
return true;
}
class RetreiveData extends AsyncTask<String,Void,List<String>> {
@Override
protected List<String> doInBackground(String... strings) {
// TODO Auto-generated method stub
try{
JSONObject jobj = clientServerInterface.makeHttpRequest("localhost/man.php");
JSONArray jsonArray = new JSONArray(localjsonString);
list = new ArrayList<String>();
if (jsonArray != null)
{
int len = jsonArray.length();
for (int i=0;i<len;i++)
{
list.add(jsonArray.get(i).toString());
}
}
else
Toast.makeText(getApplicationContext(),"Array is Null",Toast.LENGTH_LONG).show();
} catch (JSONException e) {
Toast.makeText(getApplicationContext(),"Error"+e.toString(),Toast.LENGTH_LONG).show();
}
return list;
}
protected void onPostExecute(List<String> list) {
ArrayAdapter<String> aa = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(aa);
}
}
}
答案 0 :(得分:0)
您可以创建一个简单的API,它将从Android应用程序获取请求,并返回包含文件及其路径的JSON响应。
例如,
PHP代码段,
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// check for tag type
if ($tag == 'get_data') {
// print the data
$user = $db->getData();
$response["error"] = FALSE;
$response["data"]["file_name"] = $user["file_name"];
$response["data"]["physical_path"] = $user["physical_path"];
echo json_encode($response);
}
}
at http://anyhost.com/API/api.php接受POST请求并使用以下JSON响应
{
"data": [
{
"file_name": "file.png",
"physical_path": "/pic/file.png"
},
{
"file_name": "file.png",
"physical_path": "/pic/file.png"
}
]
}
现在只需解析此JSON并在您的应用中显示它。请参阅此处的示例