这是代码,应该从指定的网站读取文本,但变量“结果”只是给我没有输出。我无法让函数main(null)运行。无论何时何地,它都会让应用程序崩溃。(我正在拨打onCreate() of MainActivity)
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;
}
}发布于 2014-05-23 10:36:06
你的代码看起来很好,我已经测试过了。它起作用了。请检查你是否给了互联网许可。将此添加到AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" />中
另外,既然你说的是"eclipse“,我想你是通过模拟器来尝试的。在这种情况下,检查机器是否在任何代理后面?如果是这样,您还需要添加适当的系统属性。
System.setProperty("http.proxyHost", "your host name/ip");
System.setProperty("http.proxyPort", "your host port");当我说它起作用时,我只是在讨论main()方法。调用PendingIntent.getBroadcast(this, 0, new Intent(result), 0);时,应确保在单击之前设置结果值,即验证是否在单击事件之前调用主方法。
https://stackoverflow.com/questions/23826029
复制相似问题