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

时间:2016-04-26 16:58:20

标签: android httpclient deprecated httpentity

我正在尝试创建一个从在线数据库获取数据的登录应用程序。我在互联网上找到了一个教程,但大多数方法都已弃用。所以我想知道如何为不推荐使用的方法找到替代方法。

在我将教程中的代码调整到我的项目之后,我在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);
    }
}

所以我得到以下的“无法解决方法/符号”:

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

您需要查看my answer to this question。我将解释如何使用HttpURLConnection发出Web请求以及如何设置必要的回调来获取响应数据。