调用时 HttpURLConnection 方法为空

HttpURLConnection method empty when called

我打电话给 (getData() 应该 运行 HttpURLConnection 和 return 一个字符串(稍后将转换为 Long),该字符串应该是 URL 中的一行: <a href="https://blockchain.info/tobtc?currency=USD&value=1" rel="nofollow">https://blockchain.info/tobtc?currency=USD&value=1</a>

试图查看它是否正在 returning 任何内容,我在 layout.xml 文件中显示 returned 字符串并显示祝酒词。但两者都显示为空白

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

我做错了什么?为什么它不 returning 字符串值。

谢谢

</p> <pre><code> 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); } }

对于主线程上的 http 请求,将 onCreate 方法更改为 StrictMode.ThreadPolicy.Builder().permitAll()

我不建议在主线程中进行 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);

}