2

我知道很多人都有像我这样的问题,但我找到的答案似乎不起作用。

import android.os.AsyncTask;



import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by DJ on 6/29/2014.
 */
public class RetrieveTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... values){
        String result;
        try{
            ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
            HttpClient httpc = new DefaultHttpClient();
            HttpPost httpp = new HttpPost("http://" + values[0] + "/AndroidConnection/Select.php");
            httpp.setEntity(new UrlEncodedFormEntity(nvp));
            HttpResponse httpr = new httpc.execute(httpp);
            HttpEntity httpe = httpr.getEntity();
            InputStream is = httpe.getContent();

            BufferedReader br = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch(Exception e){
            return e.getMessage();
        }

        return result;
    }

}
`

正如您在这段代码中看到的,我使用了 HttpClient.execute 命令,但 Android Studio 将 .execute 命令称为错误:cannot resolve symbol 'execute'

我已经尝试过使缓存无效并重新启动。我还打开了 Maven 的自动导入复选框。

操作系统:win7 32 位 JDK:jdk7(Java EE) Android Studio:8.0 beta

4

1 回答 1

1

删除new此行中的:

HttpResponse httpr = new httpc.execute(httpp);

您不必创建新对象。你只会调用方法execute

于 2014-06-29T14:04:36.437 回答