你好我的工作,它解析從他們的API一些JSON
的應用程序。我得到這個工作,這是顯示所有的信息。我現在面臨的問題是,我似乎無法得到來自JSON String
的"FileID"
:循環,並創建一個陣列從JSON對象
輸出是這樣的:
{ error: "", <fileId>: { infoToken: <string>, deleteToken: <string>, size: <int>, sha1: <string>, filename: <string> } }
的"fileID"
隨機爲每個文件生成。我怎樣才能抓住並解析它?我得到所有其他信息就好了,只需要獲得"fileId"
所以,當這樣做,我想收集所有的信息,並把它放到ListView
,我需要循環通過收到的每一個項目。
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;
}
}
嗨,我不能更改來電JSON,它的API我問過他們,但他們不會... –