How to send a JSON object over Request with Android?

前端 未结 8 2004
慢半拍i
慢半拍i 2020-11-22 03:32

I want to send the following JSON text

{\"Email\":\"aaa@tbbb.com\",\"Password\":\"123456\"}

to a web service and read the response. I kno

8条回答
  •  失恋的感觉
    2020-11-22 03:54

    public void postData(String url,JSONObject obj) {
        // Create a new HttpClient and Post Header
    
        HttpParams myParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(myParams, 10000);
        HttpConnectionParams.setSoTimeout(myParams, 10000);
        HttpClient httpclient = new DefaultHttpClient(myParams );
        String json=obj.toString();
    
        try {
    
            HttpPost httppost = new HttpPost(url.toString());
            httppost.setHeader("Content-type", "application/json");
    
            StringEntity se = new StringEntity(obj.toString()); 
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httppost.setEntity(se); 
    
            HttpResponse response = httpclient.execute(httppost);
            String temp = EntityUtils.toString(response.getEntity());
            Log.i("tag", temp);
    
    
        } catch (ClientProtocolException e) {
    
        } catch (IOException e) {
        }
    }
    

提交回复
热议问题