我是android中的新手,所以对android的了解并不是很多,好吧,让我们直截了当地说,我需要通过用户名和密码进行登录,因为我必须通过json进行http请求,作为回报,响应也将以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);
        }

08-16 05:02