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



显然我在这里遗漏了一些东西-我已经通过开发人员。关于asynctask和线程的android参考和指导材料-加上这里的帖子的分数,这也不是我的第一个牛仔竞技。我有一个内部AsyncTask类,从url抓取JSON并解析它。我一直遇到访问问题,似乎无法得到正确的组合。下面是主公共类"Consumer"的一部分代码。我可以添加更多的代码,但我相信这将演示需要什么。蒂娅!

// 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(字符串…)

…然而,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

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

public JSONObject doInBackground(String... url)

这是因为doInBackground()接受无限数量的参数。要访问所需的String,可以执行

String currentUrl = url [0];
同样,你的onPostExecute需要一个JSONObject作为参数。这将不会使用...,因为onPostExecute()只接受1个参数。

相关内容

  • 没有找到相关文章

最新更新