java– 来自androidhive教程的JSONParser,DefaultHttpClient中的NoSuchMethodError

java– 来自androidhive教程的JSONParser,DefaultHttpClient中的NoSuchMethodError,第1张

概述我正在关注this教程,并收到此错误:Causedby:java.lang.NoSuchMethodError:Novirtualmethodexecute(Lorg/apache/http/client/methods/HttpUriRequest;)Lorg/apache/http/client/methods/CloseableHttpResponse;inclassLorg/apache/http/impl/client/DefaultHttpClient;o

我正在关注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.androIDhive.materialtabs.adpater.JsONParser.makehttpRequest(JsONParser.java:52)                at info.androIDhive.materialtabs.UserFunctions.loginUser(UserFunctions.java:37)                at info.androIDhive.materialtabs.activity.MainActivity$Login.doInBackground(MainActivity.java:551)                at info.androIDhive.materialtabs.activity.MainActivity$Login.doInBackground(MainActivity.java:519)

这是我正在使用的JsONParser类:

public class JsONParser {     static @R_404_5983@Stream 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 @R_404_5983@StreamReader(                        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;        }}

解决方法:

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.Buffered@R_404_5983@Stream;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.@R_404_5983@Stream;import java.io.@R_404_5983@StreamReader;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            @R_404_5983@Stream in = new Buffered@R_404_5983@Stream(conn.get@R_404_5983@Stream());            BufferedReader reader = new BufferedReader(new @R_404_5983@StreamReader(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的AsyncTask示例:

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的示例AsyncTask:

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

总结

以上是内存溢出为你收集整理的java – 来自androidhive教程的JSONParser,DefaultHttpClient中的NoSuchMethodError全部内容,希望文章能够帮你解决java – 来自androidhive教程的JSONParser,DefaultHttpClient中的NoSuchMethodError所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104046.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存