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

Posted

技术标签:

【中文标题】有人可以帮我修复我的 android AsyncTask 吗?【英文标题】:can someone help me to fix my android AsyncTask? 【发布时间】:2014-02-06 09:10:38 【问题描述】:

我正在尝试检查服务器是否可用并且收到此错误:

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)

这是我的代码:

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!!!");
                    

                
            

        
    );

【问题讨论】:

第 305 行在哪里 你还没有添加协议。在您的网址前添加 http://。 @atebsy:这个建议有效吗? 是的,谢谢它的工作问题是我只需要添加“http://”的协议 但我还在论坛上找到了另一个代码,它运行良好且时间较短 【参考方案1】:

您传递的 URI 的问题,即,

String host = "my_url";

Refer this , this

【讨论】:

【参考方案2】:

目标主机不能为空,也不能在参数中设置。方案=null,主机=null,路径=www.novusmedia.co.za

日志中的这一行说 url 不正确。请检查是否添加了“http://”

【讨论】:

【参考方案3】:
@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();

【讨论】:

以上是关于有人可以帮我修复我的 android AsyncTask 吗?的主要内容,如果未能解决你的问题,请参考以下文章

有人可以帮我修复这个 Xcode 的 UICollection 吗?

有人可以帮我找出我的代码有啥问题吗?

android中有啥方法可以使用FFMPEG库过滤掉噪声(低通滤波器)。有人能帮我吗

我无法在我的模拟器上运行这个应用程序有人能帮我吗

找不到我的页面,有人可以帮我吗?

有人可以帮我弄清楚我的查询是不是有误吗?