另一个AsyncTask问题 - 内部类无法看到doInBackground方法

时间:2012-12-29 17:09:04

标签: android json class android-asynctask

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

// 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

1 个答案:

答案 0 :(得分:2)

您需要更改方法签名,以便

public JSONObject doInBackground(String... url)

这是因为doInBackground()接受了无数的论点。要访问您想要的String,您可以执行

String currentUrl = url [0];

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