JSON Parse Crash

时间:2014-08-07 00:17:30

标签: android json

我不确定我做错了什么。我试图稍微关注一下YouTube教程,但总是说“不幸的是,(APP NAME)已停止”。当我点击按钮时,我希望它以最新的比特币价格更新。

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

TextView price;
HttpClient client;
Button refreshPriceButton;
JSONObject json;


final static String URL = "http://api.coindesk.com/v1/bpi/currentprice.json";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    price = (TextView) findViewById(R.id.tvPrice);
    refreshPriceButton = (Button) findViewById(R.id.bJSON);
    client = new DefaultHttpClient();



    refreshPriceButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new Read().execute("rate");
        }
    });

}

public JSONObject price() throws ClientProtocolException, IOException, JSONException {
    StringBuilder url = new StringBuilder(URL);
    JSONObject coinPrice = null;

    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if(status==200) {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray coin = new JSONArray(data);
        coinPrice = coin.getJSONObject(0);
        Toast.makeText(MainActivity.this, "success", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();
    }
    return coinPrice;
}

public class Read extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            json = price();
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        price.setText(result);


    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

它在logcat中给我这些错误:

http://i.imgur.com/BPyGtrh.png

http://i.imgur.com/owmROLF.png

1 个答案:

答案 0 :(得分:0)

检查最后4行中的http://i.imgur.com/BPyGtrh.png logcat,您需要向Manifest.xml添加权限

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />