이것은 지정된 웹 사이트에서 텍스트를 읽어야하는 코드이지만 변수 "result"는 단순히 결과를 출력하지 않습니다. 함수 main (null)을 실행할 수 없습니다. 앱이 호출 될 때마다 앱이 중단됩니다. (MainActivity의 onCreate()를 호출 중입니다)Eclipse android에서 웹 페이지 읽기
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;
}
}
문제는 내가 ConvertURLToString.main (null)을 실행할 수 없다는 것입니다. 단순히 앱을 충돌시킵니다. 누구든지 웹 콘텐츠를 읽는 코드의 오류를 도와 줄 수 있습니까? – user3668388