【问题标题】:another AsyncTask issue - inner class can't see doInBackground method另一个 AsyncTask 问题 - 内部类看不到 doInBackground 方法
【发布时间】:2012-12-14 13:45:47
【问题描述】:

显然我在这里缺少一些东西 - 我已经阅读了关于 AsyncTasks 和线程的 developer.android 参考和指南材料 - 以及这里的大量帖子,这也不是我的第一个牛仔竞技表演。 我有一个内部 AsyncTask 类,它从 url 中获取 JSON 并解析它。我一直遇到访问问题,似乎无法获得正确的组合。这是主要公共类中的一段代码,即“消费者”。我可以添加更多代码,但我相信这将证明需要什么。蒂亚!

// some method
if(arg1==b){
    b.setClickable(false);  
    a.setClickable(true); 
    // next, build the url
    srchStr = urla + "&q=upc:" + value1 + urlc;
    // execute the parser
    new JSONParser().execute(value1);
}

// ...

static class JSONParser extends AsyncTask<String, Void, JSONObject> {
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = "";      


    public JSONObject doInBackground(String url) {    //getJSONFromUrl
        // Making HTTP request 
        try { 
            // defaultHttpClient 
            DefaultHttpClient httpClient = new DefaultHttpClient(); 
            HttpPost httpPost = new HttpPost(url);    
            HttpResponse httpResponse = httpClient.execute(httpPost); 
            HttpEntity httpEntity = httpResponse.getEntity(); 
            is = httpEntity.getContent();            

        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } catch (ClientProtocolException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 

        try { 
            BufferedReader reader = new BufferedReader(new InputStreamReader( 
                    is, "iso-8859-1"), 8); 
            StringBuilder sb = new StringBuilder(); 
            String line = null; 
            while ((line = reader.readLine()) != null) { 
                sb.append(line + "\n"); 
            } 
            is.close(); 
            json = sb.toString(); 
        } catch (Exception e) { 
            Log.e("Buffer Error", "Error converting result " + e.toString()); 
        } 

        // try to parse the string to a JSON object 
        try { 
            jObj = new JSONObject(json); 
        } catch (JSONException e) { 
            Log.e("JSON Parser", "Error parsing data " + e.toString()); 
        } 

        // return JSON String 
        return jObj;      
    } 
    protected void onPostExecute(String result) {
        //TextView tv2 = (TextView) findViewById(R.id.tv2);
        tv2.setText("json"); // txt.setText(result);


    }       
}

在线出现错误:

static class JSONParser extends AsyncTask<String, Void, JSONObject> {

这是:

Consumer.JSONParser 类型必须实现继承的抽象 方法 AsyncTask.doInBackground(String...)

....然而,doInBackground 方法在那里.....

我希望这些信息足以解决此问题。我希望这是我一直缺少的一些简单的东西......

更新: 我进行了建议的更改,因为它们确实有意义,但是它现在可以编译,但不会运行。请再看看,如果我弄错了,或者如果这是另一个问题,请告诉我...... 这是更新的代码:

if(arg1==b){
    b.setClickable(false);  
    a.setClickable(true); 
    // next, build the url
    srchStr = urla + "&q=upc:" + value1 + urlc;
    // execute the parser
    new JSONParser().execute(value1);
    }   
}

    static class JSONParser extends AsyncTask<String, Void, JSONObject> {
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = "";      


     public JSONObject doInBackground(String... urls) {       //getJSONFromUrl
         Consumer.srchStr = urls[0];
        // Making HTTP request 
        try { 
            // defaultHttpClient 
            DefaultHttpClient httpClient = new DefaultHttpClient(); 
            HttpPost httpPost = new HttpPost(srchStr);    
            HttpResponse httpResponse = httpClient.execute(httpPost); 
            HttpEntity httpEntity = httpResponse.getEntity(); 
            is = httpEntity.getContent();            

        } catch (UnsupportedEncodingException e) { 
            e.printStackTrace(); 
        } catch (ClientProtocolException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 

        try { 
            BufferedReader reader = new BufferedReader(new InputStreamReader( 
                    is, "iso-8859-1"), 8); 
            StringBuilder sb = new StringBuilder(); 
            String line = null; 
            while ((line = reader.readLine()) != null) { 
                sb.append(line + "\n"); 
            } 
            is.close(); 
            json = sb.toString(); 
        } catch (Exception e) { 
            Log.e("Buffer Error", "Error converting result " + e.toString()); 
        } 

        // try to parse the string to a JSON object 
        try { 
            jObj = new JSONObject(json); 
        } catch (JSONException e) { 
            Log.e("JSON Parser", "Error parsing data " + e.toString()); 
        } 

        // return JSON String 
        return jObj;      
    } 
    protected void onPostExecute(JSONObject result) {
        //TextView tv2 = (TextView) findViewById(R.id.tv2);
        result = jObj;
        tv2.setText("result"); // txt.setText(result);          
    }       
}
}

和 logcat 文本:

12-29 21:35:25.530: W/dalvikvm(2237): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
12-29 21:35:25.605: E/AndroidRuntime(2237): FATAL EXCEPTION: AsyncTask #1
12-29 21:35:25.605: E/AndroidRuntime(2237): java.lang.RuntimeException: An error occured while executing doInBackground()
12-29 21:35:25.605: E/AndroidRuntime(2237):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.lang.Thread.run(Thread.java:1096)
12-29 21:35:25.605: E/AndroidRuntime(2237): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
12-29 21:35:25.605: E/AndroidRuntime(2237):     at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:561)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at com.clicsys.pbuster.Consumer$JSONParser.doInBackground(Consumer.java:116)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at com.clicsys.pbuster.Consumer$JSONParser.doInBackground(Consumer.java:1)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-29 21:35:25.605: E/AndroidRuntime(2237):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-29 21:35:25.605: E/AndroidRuntime(2237):     ... 4 more

【问题讨论】:

  • 消费者的第116行是什么?还要仔细检查您传递给 AsyncTask 的内容,它是否正确格式化为 httpClient 想要的?
  • 116: HttpResponse httpResponse = httpClient.execute(httpPost);
  • 你将value1 传递给你的AsyncTask,当你可能需要传递srchStr 时,因为我看到它有更多的格式。我不确定这是否是您想要的,但也许这就是httpClient 大发雷霆的原因。
  • Ahhh...我的错误在 115 中,s/b: HttpPost httpPost = new HttpPost(urls[0]);.....这是你的意思吗?
  • 好吧,httpClient 抱怨出了点问题,httpPost 是你传递给execute 的最有可能的东西。仔细检查这是否有效(不是我知道该怎么做,我没有使用过这些类)。

标签: android json class android-asynctask


【解决方案1】:

你需要改变你的方法签名,所以它是

public JSONObject doInBackground(String... url)

这是因为doInBackground() 接受无限数量的参数。要访问您想要的String,您可以这样做

String currentUrl = url [0];

此外,您的 onPostExecute 需要 JSONObject 作为参数。这不会使用...,因为onPostExecute() 只接受1 个参数。

【讨论】:

  • 如果 OP 在 doInBackground 之前添加 @override 那么我认为所有问题都会得到解决
  • 添加@override 会导致同样的错误,告诉您doInBackground(String) 不会覆盖任何超类型方法,因为它是doInBackground(String...)(几乎等同于doInBackground(String[])
  • @ρяσѕρєяK Eclipse 应该这样做,即使没有删除错误的方法,因为缺少所需的方法。 doInBackground(String) 就像您可以添加到实现中的任何其他方法一样,因为它具有不同的签名(方法名称 + 参数)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多