Hi i am working on an application which parses some JSON from their API. I got this working and it is shows all the information. The problem i am facing now is that i cant seem to get the "FileID" from the JSON String:
The output is something like this:
{ error: "", <fileId>: { infoToken: <string>, deleteToken: <string>, size: <int>, sha1: <string>, filename: <string> } }
The "fileID" is randomly generated for each file. how can i catch and parse that? i get all the other information just fine, only need to get the "fileId"
So, when thats done i want to collect all the information and put it into a ListView, i need to loop through every item received.
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());
}
}
}
Any help is much appreciated!
Edit: tried this, dont know if it will work tho:
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);
}
and my adapter:
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;
}
}
fileObject:
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;
}
}