我正在遵循本教程,通过引用Yahoo Finance来构建货币转换器: http://www.firstamong.com/building-android-currency-converter/
在下载代码时,应用程序运行完美。 但是当我将这些代码复制到我自己的项目中时。它不起作用。点击转换按钮后,应用程序将关闭。
我想问一下,是不是YQL api只允许一个项目? 如果我想申请另一个api密钥怎么办?我应该修改哪部分编码? 我正在学习这种货币转换器。希望可以得到帮助。
public class CurrencyActivity extends Activity {
public int to;
public int from;
public String [] val;
public String s;
public Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currency);
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.name, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
val = getResources().getStringArray(R.array.value);
s1.setAdapter(adapter);
s2.setAdapter(adapter);
s1.setOnItemSelectedListener(new spinOne(1));
s2.setOnItemSelectedListener(new spinOne(2));
Button b = (Button) findViewById(R.id.button1);
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
{
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();
}
}
}
});
TextView credit = (TextView) findViewById(R.id.tv1);
credit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://finance.yahoo.com"));
startActivity(i);
}
});
}
public String getJson(String url)throws ClientProtocolException, IOException {
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
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();
}
private class spinOne implements OnItemSelectedListener
{
int ide;
spinOne(int i)
{
ide =i;
}
public void onItemSelected(AdapterView<?> parent, View view,
int index, long id) {
if(ide == 1)
from = index;
else if(ide == 2)
to = index;
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
更新 - 记录
03-05 00:19:08.531: D/dalvikvm(4432): GC_FOR_ALLOC freed 398K, 17% free 10431K/12504K, paused 15ms, total 16ms
03-05 00:19:08.576: D/dalvikvm(4432): GC_FOR_ALLOC freed 278K, 17% free 10926K/13076K, paused 14ms, total 14ms
03-05 00:19:08.646: D/dalvikvm(4432): GC_FOR_ALLOC freed 466K, 17% free 11765K/14080K, paused 14ms, total 14ms
03-05 00:19:08.686: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.691: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.691: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.696: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.696: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.696: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.696: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.696: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.696: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.701: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.746: D/libEGL(4432): loaded /system/lib/egl/libEGL_mali.so
03-05 00:19:08.746: D/libEGL(4432): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-05 00:19:08.751: D/libEGL(4432): loaded /system/lib/egl/libGLESv2_mali.so
03-05 00:19:08.756: E/(4432): Device driver API match
03-05 00:19:08.756: E/(4432): Device driver API version: 23
03-05 00:19:08.756: E/(4432): User space API version: 23
03-05 00:19:08.756: E/(4432): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Nov 29 14:18:37 KST 2013
03-05 00:19:08.846: D/OpenGLRenderer(4432): Enabling debug mode 0
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.871: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.876: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.876: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:08.876: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.636: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.636: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.641: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.641: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.641: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.646: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.646: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.646: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.711: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.711: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.711: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.716: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.716: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.716: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.716: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.716: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:11.716: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.301: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.316: D/AbsListView(4432): Get MotionRecognitionManager
03-05 00:19:13.411: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:13.411: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:13.411: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:13.411: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.411: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.411: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.416: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.416: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.416: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.416: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.416: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:13.436: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:14.406: D/AbsListView(4432): onDetachedFromWindow
03-05 00:19:14.441: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:14.451: D/TextLayoutCache(4432): Enable myanmar Zawgyi converter
03-05 00:19:14.461: E/ViewRootImpl(4432): sendUserActionEvent() mView == null
03-05 00:19:15.026: D/AbsListView(4432): Get MotionRecognitionManager
03-05 00:19:15.131: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:15.131: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:15.136: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:15.156: D/AbsListView(4432): unregisterIRListener() is called
03-05 00:19:15.741: D/AbsListView(4432): onDetachedFromWindow
03-05 00:19:15.761: W/InputEventReceiver(4432): Attempted to finish an input event but the input event receiver has already been disposed.
03-05 00:19:15.771: E/ViewRootImpl(4432): sendUserActionEvent() mView == null
03-05 00:19:16.861: D/AndroidRuntime(4432): Shutting down VM
03-05 00:19:16.861: W/dalvikvm(4432): threadid=1: thread exiting with uncaught exception (group=0x41cf5700)
03-05 00:19:16.871: E/AndroidRuntime(4432): FATAL EXCEPTION: main
03-05 00:19:16.871: E/AndroidRuntime(4432): android.os.NetworkOnMainThreadException
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
03-05 00:19:16.871: E/AndroidRuntime(4432): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-05 00:19:16.871: E/AndroidRuntime(4432): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-05 00:19:16.871: E/AndroidRuntime(4432): at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
03-05 00:19:16.871: E/AndroidRuntime(4432): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-05 00:19:16.871: E/AndroidRuntime(4432): at com.example.test.CurrencyActivity.getJson(CurrencyActivity.java:101)
03-05 00:19:16.871: E/AndroidRuntime(4432): at com.example.test.CurrencyActivity$1.onClick(CurrencyActivity.java:64)
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.view.View.performClick(View.java:4475)
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.view.View$PerformClick.run(View.java:18786)
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.os.Handler.handleCallback(Handler.java:730)
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.os.Handler.dispatchMessage(Handler.java:92)
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.os.Looper.loop(Looper.java:137)
03-05 00:19:16.871: E/AndroidRuntime(4432): at android.app.ActivityThread.main(ActivityThread.java:5493)
03-05 00:19:16.871: E/AndroidRuntime(4432): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 00:19:16.871: E/AndroidRuntime(4432): at java.lang.reflect.Method.invoke(Method.java:525)
03-05 00:19:16.871: E/AndroidRuntime(4432): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
03-05 00:19:16.871: E/AndroidRuntime(4432): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
03-05 00:19:16.871: E/AndroidRuntime(4432): at dalvik.system.NativeStart.main(Native Method)