Android Rest Client

后端 未结 4 1055
青春惊慌失措
青春惊慌失措 2020-12-15 13:46

I found so many samples for requesting a REST API, but all together are confusing, can some one please explain me a way to use http requests.

My Requirement is, I wa

4条回答
  •  萌比男神i
    2020-12-15 14:29

    I thing you should try this,

    HttpContext localContext = new BasicHttpContext();
    HttpClient client = new DefaultHttpClient();  
    HttpPost post = new HttpPost("REST API url"); 
    post.setHeader("Content-type", "application/json");
    
    JSONObject obj = new JSONObject();
    obj.put("username", "un");
    obj.put("pwd", "password");
    obj.put("key","123456");
    
    post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
    HttpResponse response = client.execute(post,localContext);
    

    Hope this will help.

提交回复
热议问题