0

在这里,我将 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;

        }
4

6 回答 6

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

于 2013-09-11T08:13:59.987 回答
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()));
于 2013-09-11T08:14:38.850 回答
0

问题:

  1. 返回类型是字符串,您正在尝试返回 HttpResponse。
  2. 响应范围仅限于 try-catch 块,仅当您在该块中声明它时。

解决方案:

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

尝试

@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);
于 2013-09-11T08:25:50.110 回答
0

您不能在后台线程中编写此行

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

因为你不能从非 ui 线程更新 UI。

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

于 2013-09-11T08:06:13.157 回答
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;

    }
于 2013-09-11T08:13:37.690 回答
0

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

return response.getStatusLine().getStatusCode()+""
于 2013-10-23T10:50:00.023 回答