Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android:Http GET请求无法连接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_772_1@
我是 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" />

解决方法

也许问题是因为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);
   }
  }
 }

大佬总结

以上是大佬教程为你收集整理的Android:Http GET请求无法连接全部内容,希望文章能够帮你解决Android:Http GET请求无法连接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。