http请求和json解析
2016-03-30 12:25
453 查看
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(
b6f2
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;
}
}
<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(
b6f2
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;
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories