【问题标题】:Android Studio cant find an replacement for the httpclient code ( its not working in android 25 anymore) [duplicate]Android Studio 找不到 httpclient 代码的替代品(它不再在 android 25 中工作)[重复]
【发布时间】:2017-02-05 13:49:43
【问题描述】:

这是不起作用的代码。我在网上找到了它,我尝试自己使用它,但是 HTTP 的代码总是带有一个 strok 槽,它不起作用。我认为它是因为它已经过时了,但我在互联网上搜索了所有内容并找不到答案,所以我想我可以在这里问它。

private class BackTask extends AsyncTask<Void,Void,Void> {
        ArrayList<String> list;
        protected void onPreExecute(){
            super.onPreExecute();
            list=new ArrayList<>();
        }
        protected Void doInBackground(Void...params){
            InputStream is=null;
            String result="";
            try{
                HttpClient httpclient=new DefaultHttpClient();
                HttpPost httppost= new HttpPost("http://10.0.2.2:8080/getInterviewees.php");
                HttpResponse response=httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                // Get our response as a String.
                is = entity.getContent();
            }catch(IOException e){
                e.printStackTrace();
            }

            //convert response to string
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    result+=line;
                }
                is.close();
                //result=sb.toString();
            }catch(Exception e){
                e.printStackTrace();
            }
            // parse json data
            try{
                JSONArray jArray =new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    JSONObject jsonObject=jArray.getJSONObject(i);
                    // add interviewee name to arraylist
                    list.add(jsonObject.getString("iname"));
                }
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void result){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

【问题讨论】:

标签: android httpclient


【解决方案1】:
android {
compileSdkVersion ...
buildToolsVersion ...
useLibrary 'org.apache.http.legacy'

defaultConfig {
    ...
}

在你的 build.gradle for app 上使用它。

【讨论】:

    【解决方案2】:

    尝试将其添加到您的 gradle 文件中。

    android {
        useLibrary 'org.apache.http.legacy'
    }
    

    【讨论】:

      【解决方案3】:

      或者您可以简单地使用 httpUrlConnection 代替...

      【讨论】:

      • 是的,我知道,但我不知道怎么做,你能举个例子吗?
      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 2020-09-30
      • 1970-01-01
      • 2015-11-06
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多