HttpURLConnection类无法连接在android系统给url [英] httpurlconnection unable to connect to url in android

查看:332
本文介绍了HttpURLConnection类无法连接在android系统给url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 httpConn.connect()除外; ,但在Android的异常显示nullmsg。任何帮助AP preciated.I运行在净豆code运行完美
我已经写结果<使用许可权的android:NAME =android.permission.INTERNET对/>
<使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/>

在menifest文件
任何方式解析RSS feed.I有方法,但因为通过异常code。结果
MainActivity

I got the exception at httpConn.connect(); but exception show nullmsg at android. Any Help is appreciated.I run the code at Net Beans it runs perfectly I already wrote
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> in menifest file Any way to parse the rss feed.I have the method but since code through the exception.
MainActivity

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView textView=(TextView)findViewById(R.id.title);
    try{
        OpenHttpConnection("http://www.business-standard.com/rss/economy-policy-102.rss");

    }catch (Exception e) {  textView.setText("OnCreate demo exception");}
       }

OpenHttpConnection

 private InputStream OpenHttpConnection(String urlString)  {
    InputStream in = null;
    TextView textView = (TextView) findViewById(R.id.title);
    textView.setText("coct");
    try {
        int response = -1;
        URL url = new URL(urlString);
        URLConnection con = url.openConnection();
        if(android.os.Debug.isDebuggerConnected()) {
            android.os.Debug.waitForDebugger();
        }

        if (!(con instanceof HttpURLConnection)) {
            textView.setText("Not An HTTP Connection");
        }
        try {
            HttpURLConnection httpConn = (HttpURLConnection) con;

            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");

            httpConn.connect();
            textView.setText("connect");
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK)
                in = httpConn.getInputStream();
            if (in == null) textView.setText("in passed");
            else textView.setText(in.toString());
        } catch (Exception ex) {
            textView.setText(ex.getMessage()+"msg");
            Log.d("Networking", ex.getLocalizedMessage());

        }
    }
    catch (Exception ex) {Log.d("Networking", ex.getLocalizedMessage()); }
    return  in;
}

在这里system_process错误文件

system_process error file here

[ https://drive.google.com /文件/ D / 0BzgBo2Rg7cjEX3pSaGc3TTdodFk /看法?USP =共享] [1]

推荐答案

尝试是这样的:

public class MainActivity extends Activity {
    private TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTextView = (TextView) findViewById(R.id.title);
        MyAsyncTask mMyAsyncTask = new MyAsyncTask();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
            mMyAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {    
            mMyAsyncTask.execute();
        }
    }
    // Getter method for TextView.
    public static TextView getTextView() {
        return MainActivity.mTextView;
    }
    // Setter method for TextView.
    public static void setTextView(TextView textView) {
        MainActivity.mTextView = textView;
    }
}

public class MyAsyncTask extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... params) {
        String result = MyClass.getResultFromRss();
        return result;
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result != null && MainActivity.getTextView() != null) {
            MainActivity.getTextView().setText(result);
        }
    }
}

public class MyClass {
    public static String getResultFromRss() {
        // Add your OpenHttpConnection method here.
        String result = <Result_from_OpenHttpConnection_method>;
        return result;
    }
}

这篇关于HttpURLConnection类无法连接在android系统给url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆