在Eclipse android中读取网页

时间:2014-05-23 09:49:40

标签: java android eclipse

这是应该从指定网站读取文本的代码,但变量“result”只是没有输出。我无法运行main(null)函数。它只是随时随地崩溃应用程序。 (我正在调用MainActivity的onCreate())

    public class ConvertUrlToString {

         public void main(String[] args) {

            try {
                webPage = "http://www.albab.pk/albab_edu/mashal/dsms.aspx";
                URL url = new URL(webPage);
                URLConnection urlConnection = url.openConnection();
                InputStream is = urlConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);

                int numCharsRead;
                char[] charArray = new char[1024];
                StringBuffer sb = new StringBuffer();
                while ((numCharsRead = isr.read(charArray)) > 0) {
                    sb.append(charArray, 0, numCharsRead);
                }
                result = sb.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        SmsManager sms = SmsManager.getDefault();
        PendingIntent piSent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
        PendingIntent piDelivered = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
        sms.sendTextMessage("+923018744545", null, "Hi There!", piSent, piDelivered);
    }

public void onReceive(Context arg0, Intent arg1) {
                // TODO Auto-generated method stub
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic Failure", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No Service", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio Off", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
                }

            }

1 个答案:

答案 0 :(得分:0)

您的代码看起来很好,我已经对它进行了测试。有用。请检查您是否已提供internet permission。将其添加到AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" />

另外,既然你说&#34; eclipse android&#34;,我假设你是通过模拟器尝试的。在这种情况下,检查您的机器是否在任何代理后面?如果是这样,您还需要添加适当的系统属性。

System.setProperty("http.proxyHost", "your host name/ip");
System.setProperty("http.proxyPort", "your host port");

当我说它有效时,我只是采用main()方法。当您调用PendingIntent.getBroadcast(this, 0, new Intent(result), 0);时,应确保在单击之前设置结果值,即验证在单击事件之前是否调用main方法。