Android货币转换器应用程序
我正在为Android开发一个大型程序,我正在尝试让程序中的货币转换器部分正常工作。完整披露:我从中找到了它,这是一个关于如何构建实时货币转换器的教程。我将发布有问题的代码,后面是logcat。出现的错误是,当我尝试从一种货币转换为另一种货币时,应用程序说它被迫停止。但是,“无效”部分起作用(两个货币字段相同的情况)。如有任何帮助,我们将不胜感激: 代码: 试试这个, 您正在其主线程上执行网络操作。这就是你变得越来越坚强的原因Android货币转换器应用程序,android,yahoo-api,currency-exchange-rates,Android,Yahoo Api,Currency Exchange Rates,我正在为Android开发一个大型程序,我正在尝试让程序中的货币转换器部分正常工作。完整披露:我从中找到了它,这是一个关于如何构建实时货币转换器的教程。我将发布有问题的代码,后面是logcat。出现的错误是,当我尝试从一种货币转换为另一种货币时,应用程序说它被迫停止。但是,“无效”部分起作用(两个货币字段相同的情况)。如有任何帮助,我们将不胜感激: 代码: 试试这个, 您正在其主线程上执行网络操作。这就是你变得越来越坚强的原因 b.setOnClickListener(new OnClickLi
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView t = (TextView) findViewById(R.id.textView4);
if(from == to)
{
Toast.makeText(getApplicationContext(), "Invalid", 4000).show();
}
else
{
GetResponseData abcd = GetResponseData(t);
abcd.execute();
}
}
});
试试这个,
您正在其主线程上执行网络操作。这就是你变得越来越坚强的原因
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView t = (TextView) findViewById(R.id.textView4);
if(from == to)
{
Toast.makeText(getApplicationContext(), "Invalid", 4000).show();
}
else
{
GetResponseData abcd = GetResponseData(t);
abcd.execute();
}
}
});
试试这个,
您正在其主线程上执行网络操作。这就是你变得越来越坚强的原因
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView t = (TextView) findViewById(R.id.textView4);
if(from == to)
{
Toast.makeText(getApplicationContext(), "Invalid", 4000).show();
}
else
{
GetResponseData abcd = GetResponseData(t);
abcd.execute();
}
}
});
试试这个,
您正在其主线程上执行网络操作。这就是你变得越来越坚强的原因
b.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView t = (TextView) findViewById(R.id.textView4);
if(from == to)
{
Toast.makeText(getApplicationContext(), "Invalid", 4000).show();
}
else
{
GetResponseData abcd = GetResponseData(t);
abcd.execute();
}
}
});
看起来您在android清单中启用了严格模式。当您在主线程上进行网络调用时,Strict模式将发出抱怨。您可以禁用严格模式,或者更好的方法是将网络调用放入异步任务。有问题的网络电话是
HttpResponse response=client.execute(httpGet)代码>
内部公共字符串getJson(字符串url)
您需要的异步任务已经存在,您只需要使用它
更改此else语句
else
{
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
t.setText(exResult);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对此
else {
new GetResponseData().execute("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
}
我希望这有帮助。快乐的编码。看起来你的android清单中有严格的模式。当您在主线程上进行网络调用时,Strict模式将发出抱怨。您可以禁用严格模式,或者更好的方法是将网络调用放入异步任务。有问题的网络电话是
HttpResponse response=client.execute(httpGet)代码>
内部公共字符串getJson(字符串url)
您需要的异步任务已经存在,您只需要使用它
更改此else语句
else
{
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
t.setText(exResult);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对此
else {
new GetResponseData().execute("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
}
我希望这有帮助。快乐的编码。看起来你的android清单中有严格的模式。当您在主线程上进行网络调用时,Strict模式将发出抱怨。您可以禁用严格模式,或者更好的方法是将网络调用放入异步任务。有问题的网络电话是
HttpResponse response=client.execute(httpGet)代码>
内部公共字符串getJson(字符串url)
您需要的异步任务已经存在,您只需要使用它
更改此else语句
else
{
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
t.setText(exResult);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对此
else {
new GetResponseData().execute("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
}
我希望这有帮助。快乐的编码。看起来你的android清单中有严格的模式。当您在主线程上进行网络调用时,Strict模式将发出抱怨。您可以禁用严格模式,或者更好的方法是将网络调用放入异步任务。有问题的网络电话是
HttpResponse response=client.execute(httpGet)代码>
内部公共字符串getJson(字符串url)
您需要的异步任务已经存在,您只需要使用它
更改此else语句
else
{
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
t.setText(exResult);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
对此
else {
new GetResponseData().execute("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
}
我希望这有帮助。快乐编码。您将获得:
NetworkOnMainThreadException
问题是您正在活动中调用函数getJson()
使用异步任务:
public class ProcessTask extends AsyncTask<Void, Integer, String>{
public ProcessTask() {
// TODO Auto-generated constructor stub
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
//your code of parsing
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url); //your yahooapi url goes here
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
}
return build.toString();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
公共类ProcessTask扩展了AsyncTask{
公共进程任务(){
//TODO自动生成的构造函数存根
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
//你的解析代码
StringBuilder build=新建StringBuilder();
HttpClient=new DefaultHttpClient();
HttpGet-HttpGet=newhttpget(url);//您的yahooapi url位于此处
HttpResponse response=client.execute(httpGet);
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader=新的BufferedReader(新的InputStreamReader(内容));
字符串con;
而((con=reader.readLine())!=null){
构建、追加(con);
}
返回build.toString();
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
}
}
您将获得:
NetworkOnMainThreadException
问题是您正在活动中调用函数getJson()
使用异步任务:
public class ProcessTask extends AsyncTask<Void, Integer, String>{
public ProcessTask() {
// TODO Auto-generated constructor stub
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
//your code of parsing
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url); //your yahooapi url goes here
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
}
return build.toString();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
公共类ProcessTask扩展了AsyncTask{
公共进程任务(){
//TODO自动生成的构造函数存根
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
}
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
//你的解析代码
StringBuilder build=新建StringBuilder();
HttpClient=new DefaultHttpClient();
HttpGet-HttpGet=newhttpget(url);//您的yahooapi url位于此处
HttpResponse response=client.execute(httpGet);
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader=新的BufferedReader(新的InputStreamReader(内容));
字符串con;
而((con=reader.readLine())!=null){
构建、追加(con);
}
返回build.toString();
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
}
}
您将获得:
NetworkOnMainThreadException
问题是您正在活动中调用函数getJson()<