java http 请求之post json数据(示例)

package  com.utils;

import  java.io.BufferedReader;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.util.List;

import  org.apache.http.HttpEntity;
import  org.apache.http.HttpResponse;
import  org.apache.http.client.ClientProtocolException;
import  org.apache.http.client.methods.HttpPost;
import  org.apache.http.entity.StringEntity;
import  org.apache.http.impl.client.DefaultHttpClient;
import  org.json.JSONArray;
import  org.json.JSONException;
import  org.json.JSONObject;
import  org.json.JSONStringer;

import  android.content.Context;
import  android.telephony.NeighboringCellInfo;
import  android.telephony.TelephonyManager;

public   class  Location {
    
public   static  String LOCATIONS_URL  =   " http://www.google.com/loc/json " ;

    
public   static  String getLocations(Context context) {
        
//  generate json request
        String jr  =  generateJsonRequest(context);

        
try  {
            DefaultHttpClient client 
=   new  DefaultHttpClient();

            StringEntity entity 
=   new  StringEntity(jr);

            HttpPost httpost 
=   new  HttpPost(LOCATIONS_URL);
            httpost.setEntity(entity);

            HttpResponse response 
=  client.execute(httpost);

            String locationsJSONString 
=  getStringFromHttp(response.getEntity());
            
            
return  extractLocationsFromJsonString(locationsJSONString);
        } 
catch  (ClientProtocolException e) {

            
// e.printStackTrace();
        }  catch  (IOException e) {
            
            
// e.printStackTrace();
        }  catch  (Exception e) {
            
// e.printStackTrace();
        }
        
return   null ;

    }

    
private   static  String extractLocationsFromJsonString(String jsonString) {
        String country 
=   "" ;
        String region 
=   "" ;
        String city 
=   "" ;
        String street 
=   "" ;
        String street_number 
=   "" ;
        
double  latitude  =   0.0 ;
        
double  longitude  =   0.0 ;
        
        
// "accuracy":901.0
         double  accuracy  =   0.0 ;
        
        
try  {
            JSONObject jo 
=   new  JSONObject(jsonString);
            JSONObject location 
=  (JSONObject) jo.get( " location " );
            latitude 
=  (Double) location.get( " latitude " );
            longitude 
=  (Double) location.get( " longitude " );
            accuracy 
=  (Double) location.get( " accuracy " );
            JSONObject address 
=  (JSONObject) location.get( " address " );

            country 
=  (String) address.get( " country " );
            region 
=  (String) address.get( " region " );
            city 
=  (String) address.get( " city " );
            street 
=  (String) address.get( " street " );
            street_number 
=  (String) address.get( " street_number " );
        } 
catch  (JSONException e) {

            
// e.printStackTrace();
        }

        
return   " ( "   +  latitude  +   " , "   +  longitude  +   " )\t "   +  country  +  region  +  city
                
+  street  +  street_number  +   " \t "   +   " 精确度: "   +  accuracy;
    }

    
//  获取所有的网页信息以String 返回
     private   static  String getStringFromHttp(HttpEntity entity) {

        StringBuffer buffer 
=   new  StringBuffer();

        
try  {
            
//  获取输入流
            BufferedReader reader  =   new  BufferedReader( new  InputStreamReader(
                    entity.getContent()));

            
//  将返回的数据读到buffer中
            String temp  =   null ;

            
while  ((temp  =  reader.readLine())  !=   null ) {
                buffer.append(temp);
            }
        } 
catch  (IllegalStateException e) {
            
            
// e.printStackTrace();
        }  catch  (IOException e) {
            
// e.printStackTrace();
        }
        
return  buffer.toString();
    }

    
private   static  String generateJsonRequest(Context context) {
        TelephonyManager manager 
=  (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        List
< NeighboringCellInfo >  cellList  =  manager.getNeighboringCellInfo();

        
if  (cellList.size()  ==   0 )
            
return   null ;

        JSONStringer js 
=   new  JSONStringer();
        
try  {

            js.object();

            js.key(
" version " ).value( " 1.1.0 " );
            js.key(
" host " ).value( " maps.google.com " );
            js.key(
" home_mobile_country_code " ).value( 460 );
            js.key(
" home_mobile_network_code " ).value( 0 );
            js.key(
" radio_type " ).value( " gsm " );
            js.key(
" request_address " ).value( true );
            js.key(
" address_language " ).value( " zh_CN " );

            JSONArray ct 
=   new  JSONArray();

            
for  (NeighboringCellInfo info : cellList) {

                JSONObject c 
=   new  JSONObject();
                c.put(
" cell_id " , info.getCid());
                c.put(
" location_area_code " , info.getLac());
                c.put(
" mobile_country_code " 460 );
                c.put(
" mobile_network_code " 0 );
                c.put(
" signal_strength " , info.getRssi());  //  获取邻居小区信号强度

                ct.put(c);
            }
            js.key(
" cell_towers " ).value(ct);
            js.endObject();
        } 
catch  (JSONException e) {
            
// e.printStackTrace();
             return   null ;
        }

        
return  js.toString().replace( " true " " True " );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值