调用时HttpURLConnection方法为空

时间:2015-08-21 09:38:24

标签: android bufferedreader httpurlconnection readline

我打电话给  (getData()  应该运行HttpURLConnection并返回一个字符串(稍后将被转换为Long),该字符串应该是此URL: https://blockchain.info/tobtc?currency=USD&value=1

中的一行

尝试查看它是否返回任何内容,我在layout.xml文件中显示返回的字符串并显示toast。但两者都显示为空白

请不要注意我在主线程上这样做的事实,我只是想让它先工作。

我做错了什么?为什么它没有返回字符串值。

感谢

    package app.com.cryptosudan.android.cryptosudan;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class MainActivity extends Activity {


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

    ImageButton sdg = (ImageButton) findViewById(R.id.imagesdg);
    ImageButton btc = (ImageButton) findViewById(R.id.imagebtc);

    final TextView display = (TextView) findViewById(R.id.display);
    String price;
    price = (getData());
    display.setText(price);
    Toast.makeText(this, price, Toast.LENGTH_LONG).show();


    sdg.setOnClickListener(sdgpage);
    btc.setOnClickListener(btcpage);

}
//to create an instance of button OnClickListener
View.OnClickListener sdgpage = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, CalculateSdg.class));
    }
};

//to create an instance of button OnClickListener
View.OnClickListener btcpage = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, CalculateBtc.class));
    }
};




public static String getData () {
    BufferedReader reader = null;

    try {
        URL url = new URL("https://blockchain.info/tobtc?currency=USD&value=1");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String line;
        while((line = reader.readLine()) !=null) {
            sb.append (line + "/n");

        }  return sb.toString();

    }catch (Exception e){
        e.printStackTrace();
        return null;
    }finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }


}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

    }

package app.com.cryptosudan.android.cryptosudan; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageButton sdg = (ImageButton) findViewById(R.id.imagesdg); ImageButton btc = (ImageButton) findViewById(R.id.imagebtc); final TextView display = (TextView) findViewById(R.id.display); String price; price = (getData()); display.setText(price); Toast.makeText(this, price, Toast.LENGTH_LONG).show(); sdg.setOnClickListener(sdgpage); btc.setOnClickListener(btcpage); } //to create an instance of button OnClickListener View.OnClickListener sdgpage = new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, CalculateSdg.class)); } }; //to create an instance of button OnClickListener View.OnClickListener btcpage = new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, CalculateBtc.class)); } }; public static String getData () { BufferedReader reader = null; try { URL url = new URL("https://blockchain.info/tobtc?currency=USD&value=1"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); StringBuilder sb = new StringBuilder(); reader = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; while((line = reader.readLine()) !=null) { sb.append (line + "/n"); } return sb.toString(); }catch (Exception e){ e.printStackTrace(); return null; }finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); return null; } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }

1 个答案:

答案 0 :(得分:1)

对于主线程上的http请求,使用StrictMode.ThreadPolicy.Builder()更改onCreate方法.allowAll()

我不推荐主线程中的Http请求!!!!

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

ImageButton sdg = (ImageButton) findViewById(R.id.imagesdg);
ImageButton btc = (ImageButton) findViewById(R.id.imagebtc);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

final TextView display = (TextView) findViewById(R.id.display);
String price;
price = (getData());
display.setText(price);
Toast.makeText(this, price, Toast.LENGTH_LONG).show();


sdg.setOnClickListener(sdgpage);
btc.setOnClickListener(btcpage);

}