Android 中 HttpClient、HttpPost、HttpResponse、HttpEntity 等的替代方法

Alternative methods for HttpClient, HttpPost, HttpResponse, HttpEntity, etc in Android

我正在尝试制作一个从在线数据库获取数据的登录应用程序。我在 Internet 上找到了一个教程,但大多数方法都已弃用。所以我想知道如何为已弃用的方法找到替代方法。

在我将教程中的代码应用到我的项目后,我的 MainActivity 上有这段代码:

public class MainActivity extends AppCompatActivity {

    Button b1;
    EditText ed1,ed2;

    public static final String USER_NAME = "USERNAME";

    String username;
    String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1 = (EditText) findViewById(R.id.editText);
        ed2 = (EditText) findViewById(R.id.editText2);
    }

    public void invokeLogin(View view){

        username = ed1.getText().toString();
        password = ed2.getText().toString();

        login(username,password);
    }

    private void login(final String username,String password){

        class LoginAsync extends AsyncTask<String, Void, String>{

            private Dialog loadingDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog = ProgressDialog.show(MainActivity.this,"Please wait","Loading...");
            }

            @Override
            protected String doInBackground(String... params){
                String uname = params[0];
                String pass = params[1];

                InputStream is = null;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username",uname));
                nameValuePairs.add(new BasicNameValuePair("password",pass));

                String result = null;

                try{
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost("http://example.com/login.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    BufferReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    result = sb.toString();
                } catch (ClientProtocolExeption e){
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e){
                    e.printStackTrace();
                }catch (IOException e){
                    e.printStackTrace();
                }

                return result;
            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

所以我得到了以下 "Cannot resolve method/symbol":

NameValuePair
BasicNameValuePair
HttpClient
DefaultHttpClient
HttpPost
httpPost.setEntity
UrlEncondedFormEntity
HttpResponse
httpClient.execute
HttpEntity
response.getEntity
entity.getContent
BufferReader
reader.readLine
ClientProtocolExeption
e.printStackTrace

我是 Android 开发的新手,我在这里迷路了。

我试过替换这个:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("username",uname));
                nameValuePairs.add(new BasicNameValuePair("password",pass));

这样(我说的对吗):

HashMap<String,String> nameValuePairs = new HashMap<>();
                nameValuePairs.put("username",params[0]);
                nameValuePairs.put("password",params[1]);

Apache HttpClient 库已从 android sdk api 级别 23 中删除。SDK 提供 HttpUrlConnection 并由 Android 推荐。

http://developer.android.com/reference/java/net/HttpURLConnection.html

例如:http://www.tutorialspoint.com/android/android_network_connection.htm

你会想看看 。我解释了如何使用 HttpURLConnection 发出 Web 请求以及如何设置必要的回调以获取响应数据。