从HTTP请求接收响应

时间:2013-12-29 10:40:29

标签: java php android httpclient httpresponse

我是Java的新手,我正在试图弄清楚如何从HTTP请求接收到PHP页面的响应。

这是我的主要HTTP代码:

class RequestTask extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
    }
}

这就是我用来发送请求的内容:

new RequestTask().execute("http://www.mywebsite.com/registercheck.php?first=" + first2 + "&last=" + last2 + "&dispname=" + display2 + "&email=" + email2 + "&password=" + password2 );

位于点击中。它完美无缺,它使用php文件将用户添加到表中。现在我怎么能让我的活动知道参赛作品成功了?有没有办法阅读回复?

我试过搜索,但也许我在这里寻找错误的答案,只是有点困惑。感谢您的帮助或指导。

1 个答案:

答案 0 :(得分:0)

您可以使用interface作为活动的回调。

定义界面。让您的活动实现界面。将界面用作cll回到活动并返回responseString

示例:

How do I return a boolean from AsyncTask?