第一个
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;
}
}