Android 显示应用程序中任何文件托管服务器上可用文件的列表视图
我正在开发android应用程序,它将在列表视图中显示任何文件托管服务上可用的文件,以便我可以下载它们。我可以接受任何文件托管服务,如果这对我有用的话。我尝试过dropbox,但不适用于我。请向我推荐代码或与此主题相关的任何内容。我甚至尝试过Apache服务,但没有成功。 谢谢你Android 显示应用程序中任何文件托管服务器上可用文件的列表视图,android,android-listview,Android,Android Listview,我正在开发android应用程序,它将在列表视图中显示任何文件托管服务上可用的文件,以便我可以下载它们。我可以接受任何文件托管服务,如果这对我有用的话。我尝试过dropbox,但不适用于我。请向我推荐代码或与此主题相关的任何内容。我甚至尝试过Apache服务,但没有成功。 谢谢你 package com.example.mangesh.comp; import org.json.JSONArray; import org.json.JSONException; import org.json.
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);
}
}
}
package com.example.mangesh.comp;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v7.app.ActionBarActivity;
导入android.view.Menu;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.List;
公共类ass_bce扩展了ActionBarActivity{
DLFiles clientServerInterface=新的DLFiles();
私有字符串localjsonString=“{\'data\”:[{\'file\u name\”:\“file.pdf\”,“physical\u path\”:\“/pic/file.png\”)”;
私有列表视图列表视图;
私人名单;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u ass\u bce);
listView=(listView)findViewById(R.id.listView1);
新的RetreiveData().execute();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u ass\u bce,menu);
返回true;
}
类RetreiveData扩展了异步任务{
@凌驾
受保护列表doInBackground(字符串…字符串){
//TODO自动生成的方法存根
试一试{
JSONObject jobj=clientServerInterface.makeHttpRequest(“localhost/man.php”);
JSONArray JSONArray=新的JSONArray(localjsonString);
列表=新的ArrayList();
if(jsonArray!=null)
{
int len=jsonArray.length();
对于(inti=0;i,您可以制作一个简单的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使用以下JSON接受POST请求和响应
{
"data": [
{
"file_name": "file.png",
"physical_path": "/pic/file.png"
},
{
"file_name": "file.png",
"physical_path": "/pic/file.png"
}
]
}
现在只需解析这个JSON并在你的应用程序中显示它
我已经按照你说的做了。但我觉得还是有点不对劲。请看一下我的代码,让我知道我哪里出了问题。我对json不熟悉,但更熟悉php,所以我无法理解你对json部分的看法。我仍然试过,有点不对劲,我想不出来。请看一下上面发布的代码,让我知道我出错的地方。当我尝试运行此程序时,错误显示“执行doInBackground()时出错”。请帮助我!!!