http请求和json解析

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>



public class MainActivity extends Activity {

private TextView  textView = null;
@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 = getHttpDataByGet("15850781443");
Log.e("result", ""+result);
paseJson(result);

String result1 = getHttByGet("15850781443");
Log.e("result1", ""+result1);
paseJson(result1);


String result2 = getHttpByPost("15850781443");
Log.e("result2", ""+result2);
paseJson(result2);



}
}.start();
}


/**
* 解析JSON数据

* @param json

* {"success": "1",
 "result": {
   "status": "ALREADY_ATT",
   "phone": "18012649525",
   "area": "0512",
   "postno": "215000",
   "att": "中国,江苏,苏州",
   "ctype": "中国电信180卡",
   "par": "1801264",
   "prefix": "180",
   "operators": "中国电信",
   "style_simcall": "中国,江苏,苏州",
   "style_citynm": "中华人民共和国,江苏省,苏州市"
 }
}
* */
private void paseJson(String json){
if(json==null){
return ;
}
try {
JSONObject JSONObject = new JSONObject(json);
if(JSONObject!=null){
if(!JSONObject.isNull("success")){
String success= JSONObject.optString("success");
if("1".equals(success)){
if(!JSONObject.isNull("result")){
JSONObject resultJSONObject = JSONObject.optJSONObject("result");
if(resultJSONObject!=null){
if(!resultJSONObject.isNull("phone")){
System.out.println(resultJSONObject.optString("phone"));
}

if(!resultJSONObject.isNull("area")){
System.out.println(resultJSONObject.optString("area"));
}

if(!resultJSONObject.isNull("att")){
System.out.println(resultJSONObject.optString("att"));
}

if(!resultJSONObject.isNull("ctype")){
System.out.println(resultJSONObject.optString("ctype"));
}
if(!resultJSONObject.isNull("par")){
System.out.println(resultJSONObject.optString("par"));
}
if(!resultJSONObject.isNull("operators")){
System.out.println(resultJSONObject.optString("operators"));
}

if(!resultJSONObject.isNull("style_simcall")){
System.out.println(resultJSONObject.optString("style_simcall"));
}

if(!resultJSONObject.isNull("style_citynm")){
System.out.println(resultJSONObject.optString("style_citynm"));
}

}
}
}
}
}

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

}


/**
*最古老的Get请求方法
*@author zhao 
*@param mobile
*/
private String getHttpDataByGet(String mobile){
String url = "http://api.k780.com:88/?app=phone.get&phone="+mobile+"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";

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;
}

/**
*标准方法 Get请求
*@author zhao 
*@param mobile
*/
private String getHttByGet(String mobile){
try {
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); 
params.add(new BasicNameValuePair("app", "phone.get"));  
params.add(new BasicNameValuePair("phone", mobile));  
params.add(new BasicNameValuePair("appkey", "10003"));  
params.add(new BasicNameValuePair("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4"));  
params.add(new BasicNameValuePair("format", "json"));  
String param = URLEncodedUtils.format(params, "UTF-8"); 
 
String url = "http://api.k780.com:88/?"+param;  
 
 
Log.e("url_1", ""+url);
 
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;
}

/**
*标准方法POST请求
*@author zhao 
*@param mobile
*/
private String getHttpByPost(String mobile){
//和GET方式一样,先将参数放入
try {  
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); 
params.add(new BasicNameValuePair("app", "phone.get"));  
params.add(new BasicNameValuePair("phone", mobile));  
params.add(new BasicNameValuePair("appkey", "10003"));  
params.add(new BasicNameValuePair("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4"));  
params.add(new BasicNameValuePair("format", "json"));
 
HttpPost postMethod = new HttpPost("http://api.k780.com:88/");   
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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值