简体   繁体   中英

What API do I require to use this method in Java?

I tried importing the following APIs to use this method but for some reason, it Java does not recognize this method.

  import org.apache.http.entity.ContentType;

  import javax.mail.internet.ContentType;  

  StringEntity(payload,ContentType.create("application/json");

Here is the complete Java code:

public void testCustomerApiIsExposed() throws JSONException {

    try {

        @SuppressWarnings("deprecation")

        HttpClient c = new DefaultHttpClient();

        HttpPost p = new HttpPost("http://myURL");



        String payload = "{id:\"" + 1 + "\","  + "method:\"" + "customerApi.getApiToken" + "\", params [\"DELETED\", \"DELETED\", \"\", \"\", \"\",\"\", \"\", false, \"\", \"\"" + "]}";


        /*What API do I need to import for this method???????*/
        p.setEntity(new StringEntity( payload,

        ContentType.create("application/json"))); 

        HttpResponse r = c.execute(p);

        BufferedReader reader = new BufferedReader(new InputStreamReader(r.getEntity().getContent(), "UTF-8"));

        StringBuilder builder = new StringBuilder();

        for (String line = null; (line = reader.readLine()) != null;) {

            builder.append(line).append("\n");

        }

        JSONTokener tokener = new JSONTokener("[" + builder.toString() +"]");

        JSONArray finalResult = new JSONArray(tokener);

        JSONObject o = finalResult.getJSONObject(0);

        String apiToken = (String) o.get("result");

    }

    catch(IOException e) {

        System.out.println(e);

    }

}

Can someone guide me here? Thanks

The class javax.mail.internet.ContentType does not have a create method. Only org.apache.http.entity.ContentType has it.

The compiler resolved the package to javax.mail.internet . To remove the ambiguity, explicitly mention the package:

StringEntity(payload,org.apache.http.entity.ContentType.create("application/json");

If your issue is on StringEntity , you need Apache HttpCore . To date, the latest version is HttpCore 4.3.1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM