package com.wizzer.tools;

import java.io.*;
import java.net.URLEncoder;

import java.util.*;

import org.apache.http.HttpResponse;

import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;

public class BaseProtocol
{
private StringBuilder sb = new StringBuilder();
private HttpClient httpClient;
private HttpPost httpRequest;
private HttpResponse response;
private List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
private static final int DIALOG1_KEY = 0;
private static final int DIALOG2_KEY = 1;

public BaseProtocol()
{
httpClient = new DefaultHttpClient();
}

/**
* *向服务器端发送请求 * *@paramurl *@throwsException
*
* @throws UnsupportedEncodingException
*/
public void pack(String url) throws Exception
{
httpClient = new DefaultHttpClient();
httpRequest = new HttpPost(url);
httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));
response = httpClient.execute(httpRequest);
}

/** *得到返回数据 * *@paramurl *@return *@throwsException */

public String parse() throws Exception
{
// TODO状态处理500200
if (response.getStatusLine().getStatusCode() == 200)
{
BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2.readLine())
{
sb.append(s);
}
}
return sb.toString();
}

/***
* 向服务器发送信息 * *@paramkey *@paramvalue
*
* @throws UnsupportedEncodingException
*/
public void addNameValuePair(String key, String value) throws UnsupportedEncodingException
{
nameValuePair.add(new BasicNameValuePair(key, URLEncoder.encode(value, HTTP.UTF_8)));
}

/** *返回JSONArray对象数据模型 * *@return *@throwsJSONException */

public JSONArray getJSON() throws JSONException
{
return new JSONArray(sb.toString());
}

}

更多相关文章

  1. android 系统受保护广播
  2. android 返回键 退出
  3. android input系统如何导入kl文件
  4. Android下免Root权限截屏
  5. Android.API.Context.getFilesDir()
  6. Android--学习笔记--02--AndroidStudio的设置
  7. 如何添加Android返回键的退出功能
  8. android 访问网络不能在主线程中进行以及在线程中操作UI的解决方
  9. Android(安卓)页面跳转(无/含有返回结果)

随机推荐

  1. 解决android 4.0 EditText无框的烦恼
  2. [导入]Android代码优化
  3. Unknown Android Packaging Problem
  4. Android(安卓)Binder 通信机制学习(四)
  5. Error:Could not find com.android.tools
  6. android登录窗口——基础编
  7. android 单元测试之dialog测试链接整理
  8. Android选项卡(TabWidget)应用
  9. 海创软件组--20200712--Butterknife与自
  10. Android防止应用崩溃