如何使用 AsyncTask 读取 HTTP 响应



我很难尝试从字符串中获取HTTP响应的正文。 因此,根据我的代码,我应该在名为"responseString"的字符串中有来自PHP页面的响应,但是由于这是在ASynchTask中,因此我无法在其他任何地方访问该字符串,那么我如何接收该字符串?

例如,我想将此字符串拍摄到文本视图中以进行测试,我该怎么做? 我是否读错了代码? 我得到的响应没有被放入该字符串中吗?

这是我的代码:

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);

    }
}

我正在使用以下内容来执行:

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

doInBackground后,onPostExecute将直接启动,在那里您可以更新 UI,因此由于您返回 responseString,那么onPostExecute将其放入 TextView 中

doInBackgound的回归是onPostExecute的回归,所以弦result是你的responseString

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    tv.setText(result);
}

我认为你的问题没有解决 yet.so,

对于日志或 Toast,请使用以下代码:

class RequestTask extends AsyncTask<String, String, String>{
String responseString = "";
@Override
protected String doInBackground(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    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);
    Log.d("Result is", responseString );
    Toast.makeText(YourClass.this , responseString , Toast.LENGTH_SHORT).show();
 }
}

最新更新