Android使用HttpURLConnection将JSON数据发布到服务器

Android使用HttpURLConnection将JSON数据发布到服务器,android,Android,由于HttpClient现在已不推荐用于API 22,我如何使用HttpUrlConnection在URL中发布JSON数据,代码如下: ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("location", arg0[1])); Http

由于HttpClient现在已不推荐用于API 22,我如何使用HttpUrlConnection在URL中发布JSON数据,代码如下:

    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

        nameValuePairs.add(new BasicNameValuePair("location", arg0[1]));            
HttpClient httpclient = new DefaultHttpClient();   
      HttpPost httppost = new HttpPost(arg0[0]);  
       HttpParams httpParameters = new BasicHttpParams();  
        httpclient = new DefaultHttpClient(httpParameters);
          try {       
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));          HttpResponse response;  
           response = httpclient.execute(httppost);    
         StatusLine statusLine = response.getStatusLine(); 
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {              Log.e("Google", "Server Responded OK");     
         } else {        
          response.getEntity().getContent().close();   
              throw new IOException(statusLine.getReasonPhrase());
             }   
     } catch (Exception e) {  
           e.printStackTrace();    
     } 

您可以使用此HTTP&SPDY客户端。它使您的代码可读性和稳定性。

你好,苏珊,欢迎来到stackoverflow。这是一个非常普通的问题,如果你用谷歌搜索它,你会发现很多答案,比如上面的链接或这个链接-。愉快的编码。@Darpan-NameValue对也不推荐使用!