在异步任务中的doInBackGround()中返回结果时出错

时间:2013-09-11 08:03:54

标签: android

这里我将doinBackground()置于问题的回复中。

@Override
protected string doInBackground(string... params) {
            // TODO Auto-generated method stub
            try {
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(
                        "http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-History-Request.jsp");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        3);
                nameValuePairs.add(new BasicNameValuePair(
                        "Vehicle registration mark from number plate",
                        "123456789"));
                nameValuePairs.add(new BasicNameValuePair("MOT test number",
                        "AP3398"));
                nameValuePairs.add(new BasicNameValuePair("MOT test number",
                        "000000"));

                post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = client.execute(post);

                /*String line = "";
                if (response != null) {
                    System.out
                    .println("***********************************************************");
                    xx.setText(EntityUtils.toString(response.getEntity()));

                }else {

                }*/

            } catch (IOException e) {
                e.printStackTrace();
            }
            return response;

        }

6 个答案:

答案 0 :(得分:1)

尝试以下

  String _response;
  try
  {
    ... //rest of the code
    HttpEntity resEntity = response.getEntity();
    _response=EntityUtils.toString(resEntity);   

  }catch (IOException e)
  {
      e.printStackTrace();
  } 
  return _repsonse; 
  // return string coz your return type of doInbackground is string

修改

     protected string doInBackground(string... params) 

应该是

     protected String doInBackground(String... params) 

并且您的返回类型也不是String

答案 1 :(得分:1)

1。您的回复类型为String,而您是returning response ( HttpResponse )

2. HttpResponse response;阻止

之前声明此Try{...

3. 更改为response = client.execute(post);

return EntityUtils.toString(response.getEntity())

4. 检查protected string doInBackgroun... string?应为String

5. 您无法更新U我参与doInBackground这应该在onPostExecute()

中完成

因此,如果您取消注释,则会生成Exception

xx.setText(EntityUtils.toString(response.getEntity()));

答案 2 :(得分:0)

你不能在后台线程

中写这一行
xx.setText(EntityUtils.toString(response.getEntity()));

因为您无法从非ui线程更新UI。

所以在onPostExecute()方法中写下这一行。

答案 3 :(得分:0)

您正在尝试返回HttpResponse块中创建的try-catch。所以它不会在外面访问。而doInBackgroung的返回类型是String。

因此,请将其更改为HttpResponse或将HttpResponse转换为字符串。

尝试以下代码。

@Override
    protected string doInBackground(string... params) {
        // TODO Auto-generated method stub

        HttpResponse response
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(
                    "http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-History-Request.jsp");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    3);
            nameValuePairs.add(new BasicNameValuePair(
                    "Vehicle registration mark from number plate",
                    "123456789"));
            nameValuePairs.add(new BasicNameValuePair("MOT test number",
                    "AP3398"));
            nameValuePairs.add(new BasicNameValuePair("MOT test number",
                    "000000"));

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            response = client.execute(post);

            /*String line = "";
            if (response != null) {
                System.out
                        .println("***********************************************************");
                xx.setText(EntityUtils.toString(response.getEntity()));

            }else {

            }*/

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return EntityUtils.toString(response.getEntity());
        } catch (ParseException e) {
          e.printStackTrace();
            } catch (IOException e) {
          e.printStackTrace();
        }
            return null;

    }

答案 4 :(得分:0)

<强>问题:

  1. 返回类型为String,您尝试返回HttpResponse。
  2. 响应范围仅限于try-catch块,因为您在该块中声明它。
  3. <强>解决方案:

    将响应转换为字符串,然后将其返回。

    尝试

    @Override
    protected string doInBackground(string... params) {
                // TODO Auto-generated method stub
                HttpResponse response = null;
                InputStream is = null;
                BufferedReader reader = null;
                StringBuilder sb = null;
    
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(
                            "http://motinfo.direct.gov.uk/internet/jsp/ECHID-Internet-History-Request.jsp");
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                            3);
                    nameValuePairs.add(new BasicNameValuePair(
                            "Vehicle registration mark from number plate",
                            "123456789"));
                    nameValuePairs.add(new BasicNameValuePair("MOT test number",
                            "AP3398"));
                    nameValuePairs.add(new BasicNameValuePair("MOT test number",
                            "000000"));
    
                    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
    
                    try {
                // executes the request and gets the response.
                response = client.execute(post);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            httpEntity = response.getEntity();
            is = httpEntity.getContent();
    
            // convert response to string
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
    
            String line = "0";
    
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
    
            return sb.toString();                 
    
            } 
    

    并在postExecute(String result)

    中更新以下内容

    //来自您的代码。

    xx.setText(result);
    

答案 5 :(得分:0)

尝试使用以下内容更合乎逻辑。

return response.getStatusLine().getStatusCode()+""