如何在Android中通过JSON发送http请求?
我是Android的新手,所以关于android的知识还不是很多,好的,请讲到重点,我需要通过用户名和密码进行
,因为我必须通过JSON进行http requset并返回响应也将以JSON格式提供。所以任何人都可以帮助我。谢谢。
回答:
试试这个代码
Button show_data;JSONObject my_json_obj;
String path,firstname,lastname;
path = "http://192.168.101.123:255/services/services.php?id=9";
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpEntity entity;
HttpResponse response = null;
HttpURLConnection urlconn;
my_json_obj = new JSONObject();
try
{
urlconn = (HttpURLConnection) new URL(path).openConnection();
urlconn.setConnectTimeout(10000);
urlconn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(urlconn.getOutputStream(), "UTF-8");
my_json_obj.put("sUserName", "test2");
my_json_obj.put("sPassword", "123456");
writer.write(my_json_obj.toString());
writer.close();
if(true)
{
String temp;
temp = WebRequestCall(my_json_obj);
//Log.i("Reply", temp);
}
以上是 如何在Android中通过JSON发送http请求? 的全部内容, 来源链接: utcz.com/qa/420802.html