如何通过JSON在Android中发送一个HTTP请求? [英] How to send a http request by JSON in Android?

查看:449
本文介绍了如何通过JSON在Android中发送一个HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的新的蜜蜂,所以对于Android的知识也不是那么广阔,好让开门见山,我需要通过用户​​名和密码,以使的登录,因为我必须使HTTP requset通过JSON作为回报,响应也随之到来的JSON。因此,谁能能帮助我请吧。谢谢。

I am new bee in Android , so the knowledge regarding android is not so vast, Ok lets come to the point, I need to make a login by username and password, for that I have to make http requset by JSON and in return the response will also come in JSON. So can anyone can HELP me please in it. THANKS.

推荐答案

尝试这个code

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);
        }

这篇关于如何通过JSON在Android中发送一个HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆