AsyncTask + Http Post

时间:2014-08-04 13:33:06

标签: android http android-asynctask

我已经实现了AsyncTask来向webService发出http请求,但是当我尝试获取服务器返回的编译器字符串时,它会给我一个错误。

以下是我认为AsyncTask的活动代码:

public String consultaPreguntaBBDD(int num, String tab)
{
    HttpAsyncTask httpAsyncTask = new HttpAsyncTask(String.valueOf(num),tab);
    /*(!)*/String resul = httpAsyncTask.execute("http://appdomain.hol.es/webService.php");
    return resul;
}   

我写的这个(!)编译器说:类型不匹配:无法从AsyncTask转换为String 这是AsyncTask类:

class HttpAsyncTask extends AsyncTask<String, Void, String> 
{
    private static String id;
    private static String te;
    public  HttpAsyncTask(String id,String te)
    {
            this.id = id;
            this.te = te;
     }
    @Override
    protected String doInBackground(String... urls) 
    {
        return POST(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) 
    {

    }

    public static String POST(String url)
    {
        InputStream inputStream = null;
        String result = "";
        try 
        {

            HttpClient httpclient = new DefaultHttpClient();

            // 2. make POST request to the given URL
            HttpPost httpPost = new HttpPost(url);
           // pass parameters in this way

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", id));
            nameValuePairs.add(new BasicNameValuePair("te", te));

            //add data
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPost);

            // 9. receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // 10. convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) 
        {
    Log.d("InputStream", e.getLocalizedMessage());
        }
        // 11. return result
        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException {
        // TODO Auto-generated method stub

        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
Log.i("http","result: "+result+"\n");
    return result;
    }
}

我有两个文件,每个类一个 谁能帮我?感谢的

2 个答案:

答案 0 :(得分:3)

问题在于:

  

httpAsyncTask.execute()

返回自身的实例,这是一个AsyncTask类。这不能转换为String,这就是您收到错误的原因。

.execute不会返回运行AsyncTask的结果 - 它只是启动它。要获得结果,您需要在工作线程完成其任务后访问它。

你应该如何解决它是完全实现:

  

protected void onPostExecute(String result)

在您的AsyncTask课程中。此函数的参数是您尝试从.execute开始获取的结果。在结果准备就绪时,您可以安全地访问它,您可以在此处使用它(调用适当的函数来处理结果,更新textview等)。这是异步方法调用背后的基本思想。您没有结果 ,但 可用时

答案 1 :(得分:-1)

编辑一些像这样的代码

public String consultaPreguntaBBDD(int num, String tab)
{
HttpAsyncTask httpAsyncTask = new HttpAsyncTask(String.valueOf(num),tab);
Object resul = httpAsyncTask.execute("http://appdomain.hol.es/webService.php");
return resul.toString();

}

httpAsyncTask.execute( “http://appdomain.hol.es/webService.php”);返回一个对象,将结果更改为objext类型,然后将其转换为字符串