簡體   English   中英

如何通過 Android 中的 JSON 發送 http 請求?

[英]How to send a http request by JSON in Android?

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也將出現在 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);
        }

我想這更像是另一種方式:您通過 HTTP 發送 JSON 數據。

看看HttpURLConnectionorg.json.*類。

你看過 Volley 和/或 Retrofit 嗎? 如果登錄不僅僅是您需要進行的操作,它們會很有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM