簡體   English   中英

接收來自HTTP請求的響應

[英]Receiving response from HTTP request

我是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文件將用戶添加到表中。 現在我應該如何讓我的活動知道輸入成功? 有沒有辦法閱讀回應?

我嘗試搜索,但也許我在這里尋找錯誤的答案,只是有些困惑。 感謝您的幫助或指導。

您可以將interface用作活動的回調。

定義一個接口。 讓您的活動實現該接口。 將接口用作返回活動的cll並返回responseString

范例:

如何從AsyncTask返回布爾值?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM