AsyncTask不显示来自服务器的消息(HttpUrlConnection)

时间:2015-08-07 17:52:29

标签: java android

所以我试图在AsyncTask类中通过HttpURLConnection读取服务器上的消息。问题是,当我发送请求从服务器读取数据时,它只是继续显示ProgresssDialog,就像它没有从服务器读取数据一样:

public class MainActivity extends Activity{

EditText name, password;
Button login;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name = (EditText) findViewById(R.id.name);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);


    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String serverURL = "http://192.168.1.1/my/text.php";
            LongOperation longOperation = new LongOperation();
            longOperation.execute(serverURL);


        }
    });



}

private class LongOperation  extends AsyncTask<String, Void, Void> {



    private String content;
    private String error = null;
    private ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    TextView uiUpdate = (TextView) findViewById(R.id.output);

    @Override
    protected void onPreExecute() {
        uiUpdate.setText("Output : ");
        dialog.setMessage("Downloading source..");
        dialog.show();
    }

    @Override
    protected Void doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]);
            HttpURLConnection client = (HttpURLConnection)url.openConnection();
            client.connect();
            InputStream inputStream = client.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            content = bufferedReader.readLine();

            bufferedReader.close();
            inputStream.close();
            client.disconnect();

        } catch (IOException e) {
            error = e.getMessage();
        }

        return null;
    }
    @Override
    protected void onPostExecute(Void unused) {
        dialog.dismiss();

        if (error != null) {

            uiUpdate.setText("Output : "+error);

        } else {

            uiUpdate.setText("Output : "+content);

        }
    }

我在通过HttpClient连接到服务器之前已经尝试过,所以这不是问题。谢谢!

3 个答案:

答案 0 :(得分:0)

尝试调试它,看看是否调用了OnPostExecute()方法。!

这似乎陷入了您的流阅读器代码中。此外,添加连接超时,例如5-10秒,以获得更好的用户体验。!

此外,您的返回类型的doInBackground()方法和OnPostExecute()方法的输入参数类型似乎存在问题。根据你的AsyncTask定义,它们都应该是字符串,不是吗?

答案 1 :(得分:0)

See this

尝试删除client.connect();

  try {
            URL url = new URL(urls[0]);
            HttpURLConnection client = (HttpURLConnection)url.openConnection();
            //remove client.connect();

int response = client.getResponse();

if(response == HttpURLConnection.HTTP_OK){
 InputStream inputStream = client.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            content = bufferedReader.readLine();

            bufferedReader.close();
            inputStream.close();
}

    finally{
                client.disconnect();
    }

        } catch (IOException e) {
            error = e.getMessage();
        }

        return null;

确保您的服务器正在运行。同时设置请求超时。

您并不总是需要显式调用connect方法来启动连接。依赖于连接的操作,如getInputStream,getOutputStream将在必要时隐式执行连接。

link

答案 2 :(得分:0)

试试这个:

private class LongOperation  extends                    AsyncTask<String, Void, String>
{
private String content;
private String error = null;
private ProgressDialog dialog = new        ProgressDialog(MainActivity.this);
TextView uiUpdate = (TextView)  findViewById(R.id.output);
@Override
protected void onPreExecute() {
uiUpdate.setText("Output : ");
dialog.setMessage("Downloading source..");
dialog.show();
}

@Override
protected Strinh doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection client =   (HttpURLConnection)url.openConnection();     client.connect();
InputStream inputStream = client.getInputStream();
BufferedReader bufferedReader = new       BufferedReader(new     InputStreamReader(inputStream));
content = bufferedReader.readLine();
bufferedReader.close();
inputStream.close();
//client.discon dialog.dismiss();
} 
catch (IOException e)
{
content = e.getMessage();
}

return content ;
}
@Override
protected void onPostExecute(String returnValue) {

if (return value != null) {

uiUpdate.setText("Output : "+error);
} 
else {
uiUpdate.setText("Output No Value Returned");
}
dialog.dismiss();
}