我在 2.2 和 2.3.3 上運行這段代碼可以運行,但是在4.0上崩潰。是不是http請求的問題,大家的意見呢?
public class Rezultat extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//http post
InputStream is=null;
try{
String url="http://google.com";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
private class DownloadFromUrlTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected String doInBackground(String... urls) {
//All your network stuff here.
return result
}
}