httpurlconnection无法连接到android中的url

时间:2015-07-09 06:33:38

标签: java android httpurlconnection

我在httpConn.connect();得到了异常,但异常在android上显示了nullmsg。感谢任何帮助。我在Net Beans上运行它完美运行的代码 我已写过<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 在清单文件中 任何解析rss feed的方法。我有方法但是因为代码通过例外 的 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错误文件

[https://drive.google.com/file/d/0BzgBo2Rg7cjEX3pSaGc3TTdodFk/view?usp=sharing][1]

1 个答案:

答案 0 :(得分:0)

尝试类似:

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