【问题标题】:HttpURLConnection method empty when calledHttpURLConnection 方法调用时为空
【发布时间】:2015-08-21 09:38:24
【问题描述】:

我打电话给 (getData() 应该运行 HttpURLConnection 并返回一个字符串(稍后将转换为 Long),该字符串应该是来自此 URL: @987654321@ 的一行

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

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

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

谢谢

    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);
}

    }

【问题讨论】:

  • 可能服务器返回错误。您应该检查响应代码。如果出错,调用 getErrorStream 而不是 getInputStream。此外,尝试调用 con.connect()
  • 还有一点,使用 HttpsUrlConnection,因为服务器 Url 是 https。如果可用,您应该发布您的 logcat。
  • 好点,谢谢,忽略了 Https 部分

标签: android bufferedreader httpurlconnection readline


【解决方案1】:

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

我不推荐主线程中的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);

}

【讨论】:

  • 经过测试并且有效,谢谢,一旦一切正常,当然会将所有内容移至免费线程
猜你喜欢
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 2021-12-17
  • 1970-01-01
相关资源
最近更新 更多