Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

java 程序报错未知原因

 public String HttpForm(String Url, Map<String, Object> DataParams, Map<String, String> Headers) throws IOException {
        HttpClient client=new HttpClient();
        PostMethod method=new PostMethod(Url);
        List<BasicNameValuePair> nameValuePairs = new ArrayList<>(DataParams.size());
        for (Map.Entry<String, String> entry : Headers.entrySet()) {
            method.setRequestHeader(entry.getKey(), entry.getValue());
        }
        for (Map.Entry<String, Object> entry : DataParams.entrySet()) {
            nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
            NameValuePair[] NameValueArray = (NameValuePair[])nameValuePairs.toArray(new NameValuePair[nameValuePairs.size()]);
            method.addParameters(NameValueArray);
        }
        client.executeMethod(method);
        InputStream InStream=method.getResponseBodyAsStream();
        BufferedReader Buff=new BufferedReader(new InputStreamReader(InStream));
        StringBuffer StrBuffer=new StringBuffer();
        String Temp="";
        while ((Temp=Buff.readLine()) != null) {
            StrBuffer.append(Temp);
        }
        String Response=StrBuffer.toString();
        return Response;
    }

报错如下

D:javasoftwarejdkinjava.exe "-javaagent:D:loading softwareIntelliJ IDEA 2020.2.3libidea_rt.jar=49187:D:loading softwareIntelliJ IDEA 2020.2.3in" -Dfile.encoding=UTF-8 -classpath D:javasoftwarejdkjrelibcharsets.jar;D:javasoftwarejdkjrelibdeploy.jar;D:javasoftwarejdkjrelibextaccess-bridge-64.jar;D:javasoftwarejdkjrelibextcldrdata.jar;D:javasoftwarejdkjrelibextdnsns.jar;D:javasoftwarejdkjrelibextjaccess.jar;D:javasoftwarejdkjrelibextjfxrt.jar;D:javasoftwarejdkjrelibextlocaledata.jar;D:javasoftwarejdkjrelibext
ashorn.jar;D:javasoftwarejdkjrelibextsunec.jar;D:javasoftwarejdkjrelibextsunjce_provider.jar;D:javasoftwarejdkjrelibextsunmscapi.jar;D:javasoftwarejdkjrelibextsunpkcs11.jar;D:javasoftwarejdkjrelibextzipfs.jar;D:javasoftwarejdkjrelibjavaws.jar;D:javasoftwarejdkjrelibjce.jar;D:javasoftwarejdkjrelibjfr.jar;D:javasoftwarejdkjrelibjfxswt.jar;D:javasoftwarejdkjrelibjsse.jar;D:javasoftwarejdkjrelibmanagement-agent.jar;D:javasoftwarejdkjrelibplugin.jar;D:javasoftwarejdkjrelib
esources.jar;D:javasoftwarejdkjrelib
t.jar;D:javaprojectEPSpiderargetclasses;C:UsersAdministrator.m2
epositoryorgapachehttpcomponentshttpclient4.5.12httpclient-4.5.12.jar;C:UsersAdministrator.m2
epositoryorgapachehttpcomponentshttpcore4.4.13httpcore-4.4.13.jar;C:UsersAdministrator.m2
epositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;C:UsersAdministrator.m2
epositorycommons-codeccommons-codec1.11commons-codec-1.11.jar;C:UsersAdministrator.m2
epositorycommons-httpclientcommons-httpclient3.1commons-httpclient-3.1.jar;C:UsersAdministrator.m2
epositoryorgjsoupjsoup1.12.1jsoup-1.12.1.jar;C:UsersAdministrator.m2
epositoryorgmongodbmongo-java-driver3.12.7mongo-java-driver-3.12.7.jar;C:UsersAdministrator.m2
epositoryorgslf4jslf4j-api2.0.0-alpha1slf4j-api-2.0.0-alpha1.jar main.Main
Exception in thread "main" java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.ArrayList.toArray(ArrayList.java:412)
    at com.company.MiddleWare.HttpForm(MiddleWare.java:52)
    at main.Main.main(Main.java:22)

Process finished with exit code 1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

变量nameValuePairs声明的是父类BasicNameValuePair,强转成子类NameValuePair是会报错的。如果类型使用是明确的,建议统一成一个就好了。合理的方式是统一成父类。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...