Android:Http GET请求无法连接

时间:2012-10-09 14:27:18

标签: android image http get

我是Android编程新手,可以使用我正在编写的程序帮助建立Http连接并显示图像。

我正在使用Wei-Meng Lee的“Beginning Android Application Development”一书。代码编译并且没有标记错误但每次运行程序时都会显示“错误连接”消息,并且不显示任何图像。

我查看了GET请求的各种代码示例,但找不到任何适用于我的代码的内容。

任何人都可以提供的帮助将非常感激,因为到目前为止,我正在努力寻找任何解决方案。

关于uses-permission的最后一行代码包含在Manifest中。

ImageView image;

private InputStream OpenHttpConnection(String urlString)
throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if(!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP Connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    }
    catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;

}

private Bitmap DownloadImage(String URL) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    }
    catch (IOException e1) {
        Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        e1.printStackTrace();
    }
    return bitmap;
}                                                                                                                                                                       

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_network);

    Bitmap bitmap = DownloadImage("http://www.mayoff.com/5-01cablecarDCP01934.jpg");
    image = (ImageView) findViewById(R.id.image);
    image.setImageBitmap(bitmap);

}

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

1 个答案:

答案 0 :(得分:2)

因为api版本,你可能遇到问题。您必须使用AsyncTask类来访问Web功能。

这可能与以下事实有关:在apis 11及以上版本中,不允许在主线程中访问网络,您可能必须使用ASYNC任务。

使用ASYNC任务的示例;

class InternetFileCheack extends AsyncTask<Object, Void, Boolean> {

  private Button btn;
  private String fileURL;
  Context c;

  public InternetFileCheack (Button imv, String url, Context ctx) {
   this.btn = imv;
   this.fileURL = url;
   this.c = ctx;
  }

  @Override
  protected Boolean doInBackground(Object... params) {
   Boolean sonuc = null;
   try {
    URL u = new URL(fileURL);
    HttpURLConnection huc = (HttpURLConnection) u.openConnection();
    huc.setRequestMethod("HEAD");
    huc.connect();
    int code = huc.getResponseCode();

    if (code == HttpURLConnection.HTTP_OK) {
     sonuc = true;
    } else {
     sonuc = false;
    }
   } catch (Exception e) {
    sonuc = false;
   }
   return sonuc;
  }

  @Override
  protected void onPostExecute(Boolean result) {
   btn.setEnabled(result);

   if (result) {
    Toast.makeText(c, "", Toast.LENGTH_LONG).show();
    btn.setVisibility(View.VISIBLE);
   } else {
    btn.setVisibility(View.GONE);
   }
  }
 }