0

Hi How do I properly make HTTP post request in android with this json based API.

curl -H "Content-type: application/json" -X POST \
-d '{    
  "service_key": "e93facc04764012d7bfb002500d5d1a6",
  "incident_key": "srv01/HTTP",
  "event_type": "trigger",
  "description": "FAILURE for production/HTTP on machine srv01.acme.com"
  }
}' \
"https://events.pagerduty.com/generic/2010-04-15/create_event.json"

I have this code but it is giving me status error of 400 - https://gist.github.com/26af7af09b509c0e8c2a#comments

mboy
  • 744
  • 7
  • 17

2 Answers2

0

Have you requested permission to access the network? See this question (possible duplicate):

Make an HTTP request with android

<uses-permission android:name="android.permission.INTERNET" />

needs to be in your manifest.

Community
  • 1
  • 1
Diego Basch
  • 12,764
  • 2
  • 29
  • 24
  • yes I have this in my manifest - – mboy Dec 10 '12 at 16:05
  • Why don't you log the exceptions in your code? The answer may be there, not logging exceptions is bad practice. – Diego Basch Dec 10 '12 at 16:06
  • Do you have suggestion why I am receiving status 400? this is my lates code and I think it has something to do with the sequestion of jason object? - https://gist.github.com/26af7af09b509c0e8c2a#comments – mboy Dec 10 '12 at 19:59
  • I am receiving this errors response: HTTP/1.1 400 Bad Request Json: {"description":"test","service_key":"c2181xxxxx3d0xxe57","event_type":"trigger"} Status code: 400 – mboy Dec 10 '12 at 20:00
0

I got some code somewhere else here and it worked... :)

public void requestAction(String service_key, String event_type, String incident_key, String description) 
{
    HttpClient hc = new DefaultHttpClient();
    String message;
    HttpPost p = new HttpPost("https://events.pagerduty.com/generic/2010-04-15/create_event.json");
    JSONObject object = new JSONObject();
    try 
    {
        object.put("service_key", service_key); 
        object.put("event_type", event_type);
        object.put("incident_key", incident_key);
        object.put("description","PI RedPhone: "+description);
    } 
    catch (Exception ex) 
    { }
    try 
    {
        message = object.toString();
        p.setEntity(new StringEntity(message, "UTF8"));
        p.setHeader("Content-type", "application/json");
        HttpResponse resp = hc.execute(p);
        Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
    } catch (Exception e) 
        {e.printStackTrace();}

}
mboy
  • 744
  • 7
  • 17