
我已经使用AsyncTask在JSON解析上编写了一个简单的代码.我只是在TextVIEw中显示响应.我不知道这是对还是错.它在GingerBread上工作,并在JellyBean上显示networkonmainthreadException.如果我使用StrictMode,则它可以在JellyBean上运行并在GingerBread上强制关闭.如何编写支持Android 3.0以上和AndroID 3.0以下的代码.
public class MainActivity extends Activity {TextVIEw tv;button b;inputStream is = null;DefaulthttpClIEnt clIEnt;httpGet get;httpResponse response;httpentity entity;StringBuffer buffer;@Suppresslint("NewAPI")@TargetAPI(Build.VERSION_CODES.GINGERBREAD)@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); // StrictMode.setThreadPolicy(policy); tv = (TextVIEw) findVIEwByID(R.ID.texty); b = (button) findVIEwByID(R.ID.buttonGET); b.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { MyOperation mytask = new MyOperation(); mytask.execute(); } });}private class MyOperation extends AsyncTask<String, VoID, String> { ProgressDialog dialog = new ProgressDialog(MainActivity.this); @OverrIDe protected voID onPreExecute() { // Todo auto-generated method stub super.onPreExecute(); dialog.setMessage("Loading..."); dialog.show(); } @OverrIDe protected String doInBackground(String... params) { // Todo auto-generated method stub clIEnt = new DefaulthttpClIEnt(); get = new httpGet("http://www.Google.com"); try { response = clIEnt.execute(get); } catch (ClIEntProtocolException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } entity = response.getEntity(); try { is = entity.getContent(); } catch (IllegalStateException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } return null; } @OverrIDe protected voID onPostExecute(String result) { // Todo auto-generated method stub super.onPostExecute(result); dialog.dismiss(); BufferedReader reader = new BufferedReader( new inputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = null; do { try { line = reader.readline(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } buffer.append(line); } while (line != null); tv.setText(buffer); }}}
解决方法:
public class MainActivity extends Activity { TextVIEw tv; button b; inputStream is = null; DefaulthttpClIEnt clIEnt; httpGet get; httpResponse response;httpentity entity;StringBuffer buffer;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //StrictMode.ThreadPolicy policy = new // StrictMode.setThreadPolicy(policy); tv = (TextVIEw) findVIEwByID(R.ID.texty); b = (button) findVIEwByID(R.ID.buttonGET); b.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { MyOperation mytask = new MyOperation(); mytask.execute(); } });}private class MyOperation extends AsyncTask<String, VoID, String> { ProgressDialog dialog = new ProgressDialog(MainActivity.this); @OverrIDe protected voID onPreExecute() { // Todo auto-generated method stub super.onPreExecute(); dialog.setMessage("Loading..."); dialog.show(); } @OverrIDe protected String doInBackground(String... params) {StringBuffer buffer = new StringBuffer(); // Todo auto-generated method stub clIEnt = new DefaulthttpClIEnt(); get = new httpGet("http://www.Google.com"); try { response = clIEnt.execute(get); } catch (ClIEntProtocolException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } entity = response.getEntity(); try { is = entity.getContent(); } catch (IllegalStateException e) { // Todo auto-generated catch block e.printstacktrace(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } BufferedReader reader = new BufferedReader( new inputStreamReader(is)); String line = null; do { try { line = reader.readline(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } buffer.append(line); } while (line != null); return buffer.tostring(); } @OverrIDe protected voID onPostExecute(String result) { // Todo auto-generated method stub super.onPostExecute(result); dialog.dismiss(); tv.setText(result); }}}否则,您正在主线程中运行读取行 *** 作
总结以上是内存溢出为你收集整理的Android严格模式全部内容,希望文章能够帮你解决Android严格模式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)