相关疑难解决方法(0)

AsyncTask Android示例

我正在阅读AsyncTask,我尝试了下面的简单程序.但它似乎没有用.我怎样才能使它工作?

public class AsyncTaskActivity extends Activity {

    Button btn;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener((OnClickListener) this);
    }

    public void onClick(View view){
        new LongOperation().execute("");
    }

    private class LongOperation extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            for(int i=0;i<5;i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            TextView txt = (TextView) findViewById(R.id.output); …
Run Code Online (Sandbox Code Playgroud)

android android-asynctask

667
推荐指数
11
解决办法
93万
查看次数

如何在Android中修复NetworkonMainThreadException?

我正在创建一个分配项目,我是Android的新手,我想从非常常见的URL访问json http://api.androidhive.info/contacts/,

问题:我正在尝试读取url并获取并解析此url返回的json,

我已经在我的AndroidManifest.xml中添加了以下行

<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)

首选项:和我的Android首选项是

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
Run Code Online (Sandbox Code Playgroud)
  1. Api等级18
  2. Android 4.3

这就是我试图读取网址的方式

static InputStream is = null;

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Run Code Online (Sandbox Code Playgroud)

错误消息

11-02 05:23:47.843: E/AndroidRuntime(2207): FATAL EXCEPTION: main
11-02 05:23:47.843: E/AndroidRuntime(2207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.countrypedia/com.me.countrypedia.MainActivity}: android.os.NetworkOnMainThreadException
11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-02 05:23:47.843: E/AndroidRuntime(2207):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-02 05:23:47.843: E/AndroidRuntime(2207):     at …
Run Code Online (Sandbox Code Playgroud)

java android networkonmainthread

14
推荐指数
3
解决办法
3万
查看次数

如何在Android中的服务中使用AsyncTask?

我试图在代码下运行,它给出了异常说法:

 java.lang.RuntimeException: Unable to start service com.example.testfeeds.UpdateWidgetService@410a33c8 with Intent {  cmp=com.example.testfeeds/.UpdateWidgetService (has extras) }: android.os.NetworkOnMainThreadException
Run Code Online (Sandbox Code Playgroud)

据我所知,Android的新版本将不允许主线程中的网络操作.人们建议我使用Async Task,但我不知道如何使用它.有人可以在下面的代码中显示我吗?

提前致谢

public class WidgetService extends Service {
/*
 * So pretty simple just defining the Adapter of the listview
 * here Adapter is ListProvider
 * */

/*@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    int appWidgetId = intent.getIntExtra(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);

    return (new ListProvider(this.getApplicationContext(), intent));
}*/

public static int numberOfItems=0;
  //numberOfItems=0;
    private static  String LOG = "testwidgets";
    ArrayList<String> feedsPubDate;
      @SuppressWarnings("deprecation")
    @Override
      public void onStart(Intent intent, int …
Run Code Online (Sandbox Code Playgroud)

service android asynchronous android-asynctask

7
推荐指数
1
解决办法
3万
查看次数