有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓hive教程中的java JSONParser,DefaultHttpClient中的NoSuchMethodError

我遵循this教程,并得到以下错误:

Caused by: java.lang.NoSuchMethodError: No virtual method execute(Lorg/apache/http/client/methods/HttpUriRequest;)Lorg/apache/http/client/methods/CloseableHttpResponse; in class Lorg/apache/http/impl/client/DefaultHttpClient; or its super classes (declaration of 'org.apache.http.impl.client.DefaultHttpClient' appears in /system/framework/ext.jar)
                at info.安卓hive.materialtabs.adpater.JSONParser.makeHttpRequest(JSONParser.java:52)
                at info.安卓hive.materialtabs.UserFunctions.loginUser(UserFunctions.java:37)
                at info.安卓hive.materialtabs.activity.MainActivity$Login.doInBackground(MainActivity.java:551)
                at info.安卓hive.materialtabs.activity.MainActivity$Login.doInBackground(MainActivity.java:519)

下面是我正在使用的JSONParser类:

public class JSONParser {
     static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
        // constructor
        public JSONParser() {
        }
        // function get json from url
        // by making HTTP POST or GET method
        public JSONObject makeHttpRequest(String url, String method,
                List<NameValuePair> params) {
            // Making HTTP request
            try {
                // check for request method
                if(method == "POST"){
                    // request method is POST
                    // defaultHttpClient
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }else if(method == "GET"){
                    // request method is GET
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                    HttpGet httpGet = new HttpGet(url);
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }           
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
            // return JSON String
            return jObj;
        }
}

共 (1) 个答案

  1. # 1 楼答案

    DefaultHttpClient在api级别22中被弃用,在api级别23中被删除

    该文档甚至已从Android文档中删除,以下是指向该文档之前所在位置的链接,您可以看到它重新指向的位置: http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html

    报价以防重新指导变更:

    Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.

    我创建了您正在使用的JSONParser类的更新版本,如下所示:

    import android.util.Log;
    import org.json.JSONException;
    import org.json.JSONObject;
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.HashMap;
    
    public class JSONParser {
    
        String charset = "UTF-8";
        HttpURLConnection conn;
        DataOutputStream wr;
        StringBuilder result;
        URL urlObj;
        JSONObject jObj = null;
        StringBuilder sbParams;
        String paramsString;
    
        public JSONObject makeHttpRequest(String url, String method,
                                          HashMap<String, String> params) {
    
            sbParams = new StringBuilder();
            int i = 0;
            for (String key : params.keySet()) {
                try {
                    if (i != 0){
                        sbParams.append("&");
                    }
                    sbParams.append(key).append("=")
                            .append(URLEncoder.encode(params.get(key), charset));
    
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                i++;
            }
    
            if (method.equals("POST")) {
                // request method is POST
                try {
                    urlObj = new URL(url);
    
                    conn = (HttpURLConnection) urlObj.openConnection();
    
                    conn.setDoOutput(true);
    
                    conn.setRequestMethod("POST");
    
                    conn.setRequestProperty("Accept-Charset", charset);
    
                    conn.setReadTimeout(10000);
                    conn.setConnectTimeout(15000);
    
                    conn.connect();
    
                    paramsString = sbParams.toString();
    
                    wr = new DataOutputStream(conn.getOutputStream());
                    wr.writeBytes(paramsString);
                    wr.flush();
                    wr.close();
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else if(method.equals("GET")){
                // request method is GET
    
                if (sbParams.length() != 0) {
                    url += "?" + sbParams.toString();
                }
    
                try {
                    urlObj = new URL(url);
    
                    conn = (HttpURLConnection) urlObj.openConnection();
    
                    conn.setDoOutput(false);
    
                    conn.setRequestMethod("GET");
    
                    conn.setRequestProperty("Accept-Charset", charset);
    
                    conn.setConnectTimeout(15000);
    
                    conn.connect();
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
    
            try {
                //Receive the response from the server
                InputStream in = new BufferedInputStream(conn.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
    
                Log.d("JSON Parser", "result: " + result.toString());
    
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            conn.disconnect();
    
            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(result.toString());
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
    
            // return JSON Object
            return jObj;
        }
    }
    

    Post的异步任务示例:

    class PostAsync extends AsyncTask<String, String, JSONObject> {
        JSONParser jsonParser = new JSONParser();
    
        private ProgressDialog pDialog;
    
        private static final String LOGIN_URL = "http://www.example.com/testPost.php";
    
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_MESSAGE = "message";
    
    
        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
    
        @Override
        protected JSONObject doInBackground(String... args) {
    
            try {
    
                HashMap<String, String> params = new HashMap<>();
                params.put("name", args[0]);
                params.put("password", args[1]);
    
                Log.d("request", "starting");
    
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);
    
                if (json != null) {
                    Log.d("JSON result", json.toString());
    
                    return json;
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        protected void onPostExecute(JSONObject json) {
    
            int success = 0;
            String message = "";
    
            if (pDialog != null && pDialog.isShowing()) {
                pDialog.dismiss();
            }
    
            if (json != null) {
                Toast.makeText(MainActivity.this, json.toString(),
                        Toast.LENGTH_LONG).show();
    
                try {
                    success = json.getInt(TAG_SUCCESS);
                    message = json.getString(TAG_MESSAGE);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
            if (success == 1) {
                Log.d("Success!", message);
            }else{
                Log.d("Failure", message);
            }
        }
    
    }
    

    Get的异步任务示例:

    class GetAsync extends AsyncTask<String, String, JSONObject> {
    
        JSONParser jsonParser = new JSONParser();
    
        private ProgressDialog pDialog;
    
        private static final String LOGIN_URL = "http://www.example.com/testGet.php";
    
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_MESSAGE = "message";
    
        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
    
        @Override
        protected JSONObject doInBackground(String... args) {
    
            try {
    
                HashMap<String, String> params = new HashMap<>();
                params.put("name", args[0]);
                params.put("password", args[1]);
    
                Log.d("request", "starting");
    
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "GET", params);
    
                if (json != null) {
                    Log.d("JSON result", json.toString());
    
                    return json;
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        protected void onPostExecute(JSONObject json) {
    
            int success = 0;
            String message = "";
    
            if (pDialog != null && pDialog.isShowing()) {
                pDialog.dismiss();
            }
    
            if (json != null) {
                Toast.makeText(MainActivity.this, json.toString(),
                        Toast.LENGTH_LONG).show();
    
                try {
                    success = json.getInt(TAG_SUCCESS);
                    message = json.getString(TAG_MESSAGE);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
            if (success == 1) {
                Log.d("Success!", message);
            }else{
                Log.d("Failure", message);
            }
        }
    
    }
    

    有关更多详细信息,请参阅我关于此代码的博客帖子:http://danielnugent.blogspot.com/2015/06/updated-jsonparser-with.html