简体   繁体   English

有人可以帮助我修复我的Android AsyncTask吗?

[英]can someone help me to fix my android AsyncTask?

I am trying to check if the server is available or not and am getting this error: 我正在尝试检查服务器是否可用,并收到此错误:

01-17 08:02:42.140: E/AndroidRuntime(10317): FATAL EXCEPTION: AsyncTask #1
01-17 08:02:42.140: E/AndroidRuntime(10317): java.lang.RuntimeException: An error occured while executing doInBackground()
01-17 08:02:42.140: E/AndroidRuntime(10317):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.lang.Thread.run(Thread.java:856)
01-17 08:02:42.140: E/AndroidRuntime(10317): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.novusmedia.co.za
01-17 08:02:42.140: E/AndroidRuntime(10317):    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at com.Novusmedia.Transact.UserFormActivity$MyAsyncTask.doInBackground(UserFormActivity.java:64)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-17 08:02:42.140: E/AndroidRuntime(10317):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

Here is my code: 这是我的代码:

TextView txtErrorMsg; TextView txtErrorMsg;

private class MyAsyncTask extends AsyncTask<String, String, JSONObject> {
    JSONObject json;

    @Override
    protected JSONObject doInBackground(String... params) {

        String host = "my_url";
        GamerFunctions gamerFunction = new GamerFunctions();

        HttpParams httpParams = new BasicHttpParams();
        int timeout = 100;
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        HttpConnectionParams.setSoTimeout(httpParams, timeout);
        HttpClient client = new DefaultHttpClient(httpParams);

        try {
            HttpGet request = new HttpGet(new URI(host));
            HttpResponse response = client.execute(request);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() == HttpStatus.SC_OK) {
                if (params.length != 3)
                    return null;
                json = gamerFunction.addNewGamer(params[0], params[1],
                        params[2]);

            } else {
                // update ui thread

            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            // txtErrorMsg.setText("timeout!!!");

        } catch (IOException e) {
            e.printStackTrace();
            // txtErrorMsg.setText("timeout!!!");

        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return json;
    }
}

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    if (netInfo != null && netInfo.isConnectedOrConnecting()
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    }
    return false;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_form);

    final boolean isOnline = isOnline();
    ImageButton btnPlay = (ImageButton) findViewById(R.id.btnSubmit);
    final EditText txtName = (EditText) findViewById(R.id.txtName);
    final EditText txtPhone = (EditText) findViewById(R.id.txtContact);
    final EditText txtpharmacy = (EditText) findViewById(R.id.txtPharmacy);
    txtErrorMsg = (TextView) findViewById(R.id.txtErromsg);

    btnPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String name = txtName.getText().toString();
            String phone = txtPhone.getText().toString();
            String pharmacy = txtpharmacy.getText().toString();

            if (name.isEmpty() || phone.isEmpty() || pharmacy.isEmpty()) {
                txtErrorMsg.setText("All fields are mandatory");
            } else {
                if (phone.length() != 10) {
                    txtErrorMsg.setText("Phone must be 10 characters");
                } else {
                    if (isOnline) {
                        txtErrorMsg.setTextColor(Color
                                .parseColor("#006600"));
                        txtErrorMsg.setText("wait game to load......");
                        new MyAsyncTask().execute(name, phone, pharmacy);
                        // startActivity(new Intent(UserFormActivity.this,
                        // GameActivity.class));
                    } else {
                        txtErrorMsg
                                .setText("please check your internet settings!!!");
                    }

                }
            }

        }
    });
}

The Problem with the URI you are passing ie., 您传递的URI的问题,即

String host = "my_url";

Refer this , this 参照 这个

Target host must not be null, or set in parameters. 目标主机不能为空或在参数中设置。 scheme=null, host=null, path=www.novusmedia.co.za scheme = null,host = null,path = www.novusmedia.co.za

this line from the logs say url is not proper. 日志中的这一行说url不正确。 please check have added "http://" 请检查是否已添加“ http://”

@Override
protected JSONObject doInBackground(String... params) {

    //replace your request url from the "my_url"
    String host = "my_url";// <= here
    GamerFunctions gamerFunction = new GamerFunctions();

    HttpParams httpParams = new BasicHttpParams();

happy coding 快乐编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以帮我把我使用的下载代码放在ASyncTask中吗? - Can someone help me put this download code that I use in ASyncTask? 有人可以帮我处理全局变量吗? - can someone help me with my global variables? 有人可以通过Android RemoteControlClient帮助我吗? - Can someone help me with Android RemoteControlClient? 有人可以帮我这个Android代码吗? - Can someone help me about this android code? 我的代码给了我这个错误有人可以帮助我吗 - my code is giving me this error can someone help me out 有人可以帮助我为何我的OnClickListener无法正常工作吗? Android的 - can someone help me with why my OnClickListener won't work? Android 我在应用程序的菜单(android studio)中添加了一个开关小部件,应用程序不断崩溃。 有人可以帮我解决吗? - I added a switch widget to the app' menu (android studio) and the app keeps crashing. Can someone help me fix it? 我无法在我的模拟器上运行这个应用程序有人可以帮助我吗 - i cant run this app on my emulator CAN someone help me 有人可以帮我自动创建闹钟吗? - Someone can help me the create a alarm android automatic? 有人可以帮我解决这个Android位置错误吗? - Can someone help me please with this android location error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM