我是编程和 android 的新手,JsonObject/JsonArray 有一些问题,我知道我很接近但缺少一些东西。
在尝试了一些解决方案和 tuts 之后,我没有错误!,即使我尝试其他 api 我也可以,但在这种情况下,应用程序正在关闭。
好的,我用这个api,这就是所有的响应
{ "target": "MXN", "success": true, "rate": 13.1035, "source": "USD", "amount": 13.1, "message": "" }
我知道我的错误在于“无法转换为 JSONObject”,但我不知道如何解决?有任何想法吗 ?
我的日志猫
org.json.JSONException: Value MXN at target of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONObject(JSONObject.java:578)
at com.example.androidjsonparser.MainActivity$ProgressTask.doInBackground(MainActivity.java:106)
at com.eaxmple.androidjsonparser.MainActivity$ProgressTask.doInBackground(MainActivity.java:1)
主要活动
package com.example.androidjsonparser;
import java.util.ArrayList;...
public class MainActivity extends ListActivity {
private Context context;
private static String url = "http://currency-api.appspot.com/api/USD/MXN.json?key=24c050085728994c96582a3000b9673f55370909";
private static final String TAG_TARGET = "target";
private static final String TAG_RATE = "rate";
private static final String TAG_SOURCE = "source";
private static final String TAG_AMOUNT = "amount";
private static final String TAG_MESSAGE = "";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ProgressTask(MainActivity.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private ListActivity activity;
// private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_item, new String[] { TAG_TARGET, TAG_RATE,
TAG_SOURCE, TAG_AMOUNT }, new int[] {
R.id.target, R.id.rate, R.id.source,
R.id.amount });
setListAdapter(adapter);
// selecting single ListView item
lv = getListView();
}
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(TAG_TARGET);
String target = c.getString(TAG_TARGET);
String rate = c.getString(TAG_RATE);
String source = c.getString(TAG_SOURCE);
String amount = c.getString(TAG_AMOUNT);
String message = c.getString(TAG_MESSAGE);
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_TARGET, target);
map.put(TAG_RATE, rate);
map.put(TAG_SOURCE, source);
map.put(TAG_AMOUNT, amount);
map.put(TAG_MESSAGE, message);
jsonlist.add(map);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
}
还有我的 JsonParser
package com.example.androidjsonparser;
import java.io.BufferedReader;...
public class JSONParser {
static InputStream is = null;
static JSONObject jarray = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e("==>", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try parse the string to a JSON object
try {
jarray = new JSONObject( builder.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jarray;
}
}