嗨,我正在开发一个JSON
从他们的 API 中解析一些内容的应用程序。我得到了这个工作,它显示了所有信息。我现在面临的问题是我似乎无法"FileID"
从JSON String
:
输出是这样的:
{ error: "", <fileId>: { infoToken: <string>, deleteToken: <string>, size: <int>, sha1: <string>, filename: <string> } }
"fileID"
是为每个文件随机生成的。我怎样才能捕捉和解析它?我得到所有其他信息就好了,只需要得到"fileId"
因此,完成后我想收集所有信息并将其放入 aListView
中,我需要遍历收到的每个项目。
public class FilesActivity extends SherlockActivity {
private static String TAG_FILENAME = "filename";
private static String TAG_SIZE = "size";
private static String TAG_ITOKEN = "infoToken";
private static String TAG_DTOKEN = "deleteToken";
private static String TAG_SHA1 = "sha1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dblist);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Files");
String response = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost("http://api.bayfiles.net/v1/account/files?session=<sessionId>");
try {
JSONObject json = new JSONObject();
json.put("filename", "error");
postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
postMethod.setHeader( "Content-Type", "application/json" );
response = httpClient.execute(postMethod,resonseHandler);
TextView txt = (TextView)findViewById(R.id.nodata);
JSONObject request = new JSONObject(response);
for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext(); ) {
String key = (String) keyIterator.next();
JSONObject object = request.optJSONObject(key);
if (object != null) {
//Setting TAGs
TAG_FILENAME = object.getString("filename");
TAG_SIZE = object.getString("size");
TAG_ITOKEN = object.getString("infoToken");
TAG_DTOKEN = object.getString("deleteToken");
TAG_SHA1 = object.getString("sha1");
txt.setText(
TAG_FILENAME + "\n"
+ TAG_SIZE + "\n"
+ TAG_ITOKEN + "\n"
+ TAG_DTOKEN + "\n"
+ TAG_SHA1 + "\n"
+ txt.getText()
);
Log.d("log_tag", object.getString("filename"));
}
}
}
catch(Exception e)
{
e.printStackTrace();
Log.d("log_tag", "Error: " + e.toString());
}
}
}
任何帮助深表感谢!
编辑:试过这个,不知道它是否会起作用:
ArrayList<fileObject> objectList = new ArrayList<fileObject>();
if (object != null) {
fileObject obj = new fileObject();
obj.setFileName(object.getString("filename"));
obj.setSize(object.getString("size"));
obj.setInfoToken(object.getString("infoToken"));
obj.setDeleteToken(object.getString("deleteToken"));
obj.setSha1(object.getString("sha1"));
objectList.add(obj);
Log.d("log_tag", object.getString("filename"));
}
adapter = mView.new fileObjectAdapter(mContext);
setListAdapter(adapter);
adapter.addAll(objectList); //if honeycomb or higher
for (fileObject obj : objectList) {
adapter.add(obj);
}
和我的适配器:
package com.dev.jsontest.adapter;
import com.dev.jsontest.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
public class fileObjectAdapter extends ArrayAdapter<fileObject> {
public fileObjectAdapter(Context context) {
super(context, 0);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.dbitems, parent, false);
}
final fileObject item = getItem(position);
//define your view that you want each list item to look like. Make sure to set every field in your view.
return convertView;
}
}
文件对象:
package com.dev.jsontest.adapter;
public class fileObject {
private String fileName;
private String size;
private String infoToken;
private String deleteToken;
private String sha1;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getInfoToken() {
return infoToken;
}
public void setInfoToken(String infoToken) {
this.infoToken = infoToken;
}
public String getDeleteToken() {
return deleteToken;
}
public void setDeleteToken(String deleteToken) {
this.deleteToken = deleteToken;
}
public String getSha1() {
return sha1;
}
public void setSha1(String sha1) {
this.sha1 = sha1;
}
}