3

同じクラスでコードを実行すると、アプリは正常に実行されます。しかし、このコードを2つのクラスで実行すると、アプリでエラーandroid.os.networkonmainthreadexceptionが発生しました。デバッグ時にresponseHttp=httpConnection.getResponseCode();でエラーを検出します。 app run to line responseHttp = httpConnection.getResponseCode(); そして、catch {}に移動し、「If..else」をキャンセルします。エラーandroid.os.networkonmainthreadexceptionをログに記録します。手伝って頂けますか!!

私のコードクラスAsynctask

package com.example.finishdemo;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.os.AsyncTask;

public class TestConnectionNew extends AsyncTask<String, Void, String> {
private int responseHttp = 0;
private String flag="false";

@Override
protected String doInBackground(String... urltest) {
    // TODO Auto-generated method stub
    try {
        URL url = new URL(urltest[0]);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(2000);
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        **responseHttp = httpConnection.getResponseCode();**
        if (responseHttp == HttpURLConnection.HTTP_OK) {
            flag = "true";
        } else {
            flag = "false";
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Thong bao loi: "+e.toString());
    }
    return flag;
}
}

コードクラスメイン:

package com.example.finishdemo;
public class Hoadon extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hoadon);
    TestConnectionNew t=new TestConnectionNew();
    String recieve=t.doInBackground("http://longvansolution.tk/monthlytarget.php");
    if(recieve.equalsIgnoreCase("true"))
    {
        doTimerTask();
    }else
        if(recieve.equalsIgnoreCase("false"))
    {
        showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!");
    }
4

2 に答える 2

7

次のようにdoInBackgroundを手動で呼び出す代わりに、 asynctask.executeを使用してAsyncTaskを実行します。

 TestConnectionNew t = new TestConnectionNew();
 t.execute("http://longvansolution.tk/monthlytarget.php");

TestConnectionNewを次のように変更します

public class TestConnectionNew extends AsyncTask<String, Void, String> {
private int responseHttp = 0;
private String flag="false";

@Override
protected String doInBackground(String... urltest) {
    // TODO Auto-generated method stub
    try {
        URL url = new URL(urltest[0]);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(2000);
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        **responseHttp = httpConnection.getResponseCode();**
        if (responseHttp == HttpURLConnection.HTTP_OK) {
            flag = "true";
        } else {
            flag = "false";
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Thong bao loi: "+e.toString());
    }
    return flag;
}

@Override
     protected void onPostExecute(String recieve) {
           if(recieve.equalsIgnoreCase("true"))
            {
               doTimerTask();
           }else
           if(recieve.equalsIgnoreCase("false"))
             {
              showAlert("Không kết nối được đến server hoặc thiết bị chưa có kết nối internet!");
             }
      }
}

AsyncTaskの使用方法の詳細については、以下を参照してください。

http://developer.android.com/reference/android/os/AsyncTask.html

于 2012-12-06T08:42:04.267 に答える
0

AndroidManifest.xmlで、コードを追加します。

uses-permission android:name="android.permission.INTERNET" 
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 
于 2013-10-17T08:41:13.493 に答える