http请求和json解析的截取字符串

第一个

public class MainJsonActivity extends FragmentActivity{


private TextView  textView = null;
private Object json;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textView = (TextView)this.findViewById(R.id.textview);

new Thread(){
public void run(){

String result = getHttpByPost("15850781443").trim();
//截取字符串
int endIndex = result.lastIndexOf(")");   
int startIndex = result.indexOf("(")+1;

String result1 = result.substring(startIndex, endIndex);


Log.e("result1", ""+result1);

try {
paseJson(result1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}.start();
}

/**
* 解析JSON数据

* @param json

* {
  mobile: '15850781443',
  province: '江苏',
  isp: '中国移动',
  stock: '1',
  amount: '10000',
  maxprice: '0',
  minprice: '0',
  cityname: '南京'
}
* @throws JSONException 
* */
private void paseJson(String json) throws JSONException {
// TODO Auto-generated method stub
if(json==null){
return ;
}


JSONObject JSONObject = new JSONObject(json);

if(JSONObject!=null){
if(!JSONObject.isNull("mobile")){
System.out.println(JSONObject.optString("mobile"));

}
if(!JSONObject.isNull("province")){
System.out.println(JSONObject.optString("province"));
}
if(!JSONObject.isNull("isp")){
System.out.println(JSONObject.optString("isp"));
}
if(!JSONObject.isNull("stock")){
System.out.println(JSONObject.optString("stock"));
}
if(!JSONObject.isNull("amount")){
System.out.println(JSONObject.optString("amount"));
}
if(!JSONObject.isNull("maxprice")){
System.out.println(JSONObject.optString("maxprice"));
}
if(!JSONObject.isNull("minprice")){
System.out.println(JSONObject.optString("minprice"));
}
if(!JSONObject.isNull("cityname")){
System.out.println(JSONObject.optString("cityname"));
}
}

}
@SuppressWarnings("deprecation")
protected String getHttpByPost(String mobile) {
// TODO Auto-generated method stub
try {     
@SuppressWarnings("deprecation")
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); 
params.add(new BasicNameValuePair("mobile", mobile));
params.add(new BasicNameValuePair("amount", "10000"));
HttpPost postMethod = new HttpPost("http://virtual.paipai.com/extinfo/GetMobileProductInfo");   
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); 
//将参数填入POST Entity中    
HttpResponse response = new DefaultHttpClient().execute(postMethod); 
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
return  EntityUtils.toString(entity);    
}
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}



}



第二个

public class HooJsonActivity extends FragmentActivity{


@SuppressWarnings("unused")
private TextView  textView = null;
@SuppressWarnings("unused")
private Object json;
@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.setContentView(R.layout.activity_main);   
      textView= (TextView)this.findViewById(R.id.text);
      new Thread(){
public void run(){

String result = getHttpDataByGet("15850781443");

Log.e("result", ""+result);

int startIndex = result.indexOf("{")-1;//截取字符串

String result1 = result.substring(startIndex);

Log.e("result1", ""+result1);

try {
paseJson(result1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}.start();
}




/**
* 解析JSON数据

* @param json

* {
  {
  mts: '1585078',
  province: '江苏',
  catName: '中国移动',
  telString: '15850781443',
  areaVid: '30511',
  ispVid: '3236139',
  carrier: '江苏移动'
}'
}
* @throws JSONException 
* */

private void paseJson(String json) throws JSONException {
// TODO Auto-generated method stub
if(json==null){
return ;
}
JSONObject Zhaojson = new JSONObject(json);


if(Zhaojson!=null){
if(!Zhaojson.isNull("mts")){
System.out.println(Zhaojson.optString("mts"));
}
if(!Zhaojson.isNull("province")){
System.out.println(Zhaojson.optString("province"));
}
if(!Zhaojson.isNull("telString")){
System.out.println(Zhaojson.optString("telString"));
}
if(!Zhaojson.isNull("areaVid")){
System.out.println(Zhaojson.optString("areaVid"));
}
if(!Zhaojson.isNull("mts")){
System.out.println(Zhaojson.optString("mts"));
}
if(!Zhaojson.isNull("ispVid")){
System.out.println(Zhaojson.optString("ispVid"));
}
if(!Zhaojson.isNull("carrier")){
System.out.println(Zhaojson.optString("carrier"));
}
}








}
@SuppressWarnings("deprecation")
private String getHttpDataByGet(String tel){
String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+tel+"&qq-pf-to=pcqq.c2c";

Log.e("url_2", ""+url);

try {
HttpResponse response = new DefaultHttpClient().execute(new HttpGet(url));
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
return  EntityUtils.toString(entity);    
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值