duanchan9354 2013-12-29 10:40
浏览 149

从HTTP请求接收响应

I am new to Java and I am trying to figure out how to receive a response from an HTTP request to a PHP page.

here is my main HTTP code:

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..
    }
}

and this is what I am using to send the request:

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

which is nestled in an on-click. It works flawlessly, it adds the user to the table using the php file. Now how am I supposed to let my activity know that the entry was successful? Is there a way to read a response?

I tried searching but maybe I am looking for the wrong answers here, just a little confused. Thank you for any help or direction.

  • 写回答

1条回答 默认 最新

  • duanjie9630 2013-12-29 10:46
    关注

    You can use interface as a callback to the activity.

    Define a interface. Let your activity implement the interface. Use the interface as a cll back to the activity and return responseString.

    Example :

    How do I return a boolean from AsyncTask?

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法