How to send a http request by JSON in Android?

前端 未结 3 684
失恋的感觉
失恋的感觉 2020-12-17 05:46

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

相关标签:
3条回答
  • 2020-12-17 06:26

    I guess it is more the other way 'round: you send JSON data over HTTP.

    Have a look at HttpURLConnection and the org.json.* classes.

    0 讨论(0)
  • 2020-12-17 06:49

    Try out this 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);
            }
    
    0 讨论(0)
  • 2020-12-17 06:50

    Have you looked at Volley and/or Retrofit? If login is not only operation you need to make they can be of much help.

    0 讨论(0)
提交回复
热议问题