我正在尝试解析json数据,在我的应用程序中,我有2个下拉框,用户可以从中选择要转换数据的国家/地区名称。他想要转换的数量将传递到URL。我正在使用AsyncHttpClient lib来读取数据,然后在不同的api上使用它完全正常,但是当我改变我的api时,它开始运行失败方法。首先我认为我通过了错误的网址。但是在吐司我得到了知道我正在传递api的正确网址。
有什么问题可以告诉我.. ??
提前感谢你。
private String API_URL = "http://devel.farebookings.com/api/curconversor/";
public void onClick(View arg0) {
final String url1 = API_URL + t1.toString() + "/" + t2.toString() + "/"
+ usdValue.getText().toString() + "/" + "json";
Toast.makeText(getApplicationContext(), url1.toString(),
Toast.LENGTH_LONG).show();
if (!usdValue.getText().toString().equals("")) {
AsyncHttpClient client = new AsyncHttpClient();
client.get(url1, new AsyncHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, String arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
Toast.makeText(getApplicationContext(),
"unsuccess", Toast.LENGTH_LONG).show();
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(getApplicationContext(), "start",
Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(String response) {
Log.i("CHACHING", "HTTP Sucess");
Toast.makeText(getApplicationContext(), "success",
Toast.LENGTH_LONG).show();
try {
JSONObject jsonObj = new JSONObject(response);
Double usds = Double.valueOf(usdValue.getText()
.toString());
Double rate = jsonObj.getDouble("to_amount");
Toast.makeText(getApplicationContext(),
String.valueOf(rate), Toast.LENGTH_LONG)
.show();
double the_result = usds * rate;
ConvertedFrom.setText(usdValue.getText()
.toString()
+ " "
+ ConvertedFromCurr.getSelectedItem()
.toString() + " = ");
ConvertedTo.setText(String.valueOf(the_result)
+ " "
+ ConvertedToCurr.getSelectedItem()
.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} else {
Toast.makeText(getApplicationContext(),
"Please enter a Amount value!", Toast.LENGTH_LONG)
.show();
}
答案 0 :(得分:0)
对于跨域api调用,您必须使用jsonp。
这是javascript中的一个例子
$.ajax({
url: "http://devel.farebookings.com/api/curconversor/EUR/GBP/1/",
dataType: 'jsonp',
success: function (data) {
alert(data.GBP);
}
});