Android ~ send parameters to http, POST method

String url = "http://ggggggggggyyyyyyyyyyyyyyy/xxxx.html";
JSONObject j = new JSONObject();

try{

j.put("param1", "asd");
j.put("param2", 2);
String qqq = makeRequest(url, j);
Log.i("xxxxxxxxxx", qqq);

}catch(Exception e){

Log.i("zzzzzz", "asdadaasd");

}

------------------------------------------------------------------------------------------

public static String makeRequest(String path, JSONObject params) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(path);

StringEntity se = new StringEntity(params.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");

HttpResponse response = httpclient.execute(httpost);
JSONObject output = new JSONObject(EntityUtils.toString(response.getEntity()));

return output.toString();

}


這個範例是用POST的方式傳參數到網址

張稿寫的~~XD

沒有留言: