asaipackage com.donson.host;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import com.donson.common.AdBackViewData;
import com.donson.common.ApplicationInfo;
import com.donson.common.bean.AdBackInfo;
import com.donson.common.bean.InitBackInfo;
import com.donson.util.Consts;
import com.donson.util.DownLoadBitmap;
import com.donson.util.LogInfo;
public class ReturnInfoParser
{
/**
* 解析从服务器中返回的数据
* @param is
* @return
*/
public static void parseReturnInfo(Context context, String responseStr, String method)
{
// String responseJson = convertStreamToString(responseStream);
JSONObject jsObject = null;
try
{
String response = new String(responseStr.getBytes(), "utf-8");
LogInfo.writeLog("ReturnInfoParser.parseReturnInfo", "-------------------------" + response);
jsObject = new JSONObject(response);
LogInfo.writeLog("ReturnInfoParser.parseReturnInfo", "-------------jsObject------------" + response);
}
catch (UnsupportedEncodingException e)
{
LogInfo.writeError("ReturnInfoParser.parseReturnInfo", e);
}
catch (JSONException e)
{
LogInfo.writeError("ReturnInfoParser.parseReturnInfo", e);
}
// 判断返回的结果是否为空
if (jsObject == null || jsObject.toString().length() == 0)
{
return;
}
else
{
if (method == Consts.INIT)
{
parseInitInfo(jsObject);
}
if (method == Consts.ZHONGWEN)
{
parseZhongWenInfo(jsObject);
}
else if (method == Consts.CLIENT)
{
parseClientInfo(jsObject);
}
else if (method == Consts.AD)
{
parseAdInfo(jsObject, context);
}
else if (method == Consts.CLICK)
{
parseClickInfo(jsObject);
}
else if (method == Consts.INFO)
{
parseInfoInfo(jsObject);
}
else if (method == Consts.LOGS)
{
parseLogsInfo(jsObject);
}
}
}
/**
* 解析请求配置信息
*/
private static void parseInitInfo(JSONObject json)
{
InitBackInfo initBackInfo = ApplicationInfo.getInstance().initBackInfo;
try
{
int mt = json.getInt("mt");
int it = json.getInt("it");
int iu = json.getInt("iu");
String rurl = json.getString("rurl");
String burl = json.getString("burl");
initBackInfo.setMt(mt);
initBackInfo.setIt(it);
initBackInfo.setIu(iu);
initBackInfo.setRurl(rurl);
initBackInfo.setBurl(burl);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* 测试中文信息
*/
private static void parseZhongWenInfo(JSONObject json)
{
try
{
int id = json.getInt("id");
String title = json.getString("title");
String content = json.getString("content");
String singer = json.getString("singer");
LogInfo.writeLog("ReturnInfoParser.parseInitInfo", "id=" + id + " " + "title=" + title + " " + "content="
+ content + " " + "singer=" + singer);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* 解析上传手机客户端基本信息
*/
private static void parseClientInfo(JSONObject json)
{
}
/**
* 解析请求广告信息
*/
private static void parseAdInfo(JSONObject json, Context context)
{
try
{
int st = json.getInt("st");
switch (st)
{
// 含文字,图标
case Consts.INT_ZERO_VALUE:
parseTextPic(json, context);
break;
// 只含图片
case Consts.INT_ONE_VALUE:
parsePic(json, context);
break;
// 只含富媒体
case Consts.INT_TWO_VALUE:
// parseTextPic(json);
break;
default:
break;
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
private static void parsePic(JSONObject json, Context context)
{
try
{
AdBackInfo adBackInfo = ApplicationInfo.getInstance().adBackInfo;
AdBackViewData adBackViewData = ApplicationInfo.getInstance().adBackViewData;
int err = json.getInt("err");
int id = json.getInt("id");
int st = json.getInt("st");
int nx = json.getInt("nx");
// 左、右、背景图片解析
JSONObject pic = json.getJSONObject("pic");
String a = pic.getString("a");
List<String> picList = new ArrayList<String>();
picList.add(a);
JSONArray conArray = json.getJSONArray("con");
List<String> urlList = new ArrayList<String>();
for (int i = 0; i < conArray.length(); i++)
{
urlList.add(conArray.getJSONObject(i).getString("img"));
}
List<ImageView> imageList = new ArrayList<ImageView>();
for (int i = 0; i < conArray.length(); i++)
{
imageList.add(new ImageView(context));
}
adBackInfo.setErr(err);
adBackInfo.setId(id);
adBackInfo.setSt(st);
adBackInfo.setNx(nx);
adBackInfo.setPic(picList);
adBackInfo.setCon(urlList);
adBackViewData.setBimageView(new ImageView(context));
adBackViewData.getBimageView().setImageBitmap(DownLoadBitmap.getInstance().getBitmap(picList.get(0)));
// BitmapTask bitmapTask = new BitmapTask(urlList, picImageList);
// bitmapTask.execute();
/*
* List<String> picList = new ArrayList<String>(); picList.add(pic);
* adBackConViewData.setConList(imageList); BitmapTask bitmapTask = new BitmapTask(urlList,
* adBackConViewData.getConList()); bitmapTask.execute();
*/
}
catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* 解析含有文字和图标的广告信息
* @param json
*/
private static void parseTextPic(JSONObject json, Context context)
{
AdBackInfo adBackInfo = ApplicationInfo.getInstance().adBackInfo;
AdBackViewData adBackViewData = ApplicationInfo.getInstance().adBackViewData;
try
{
int err = json.getInt("err");
int id = json.getInt("id");
int nx = json.getInt("nx");
int st = json.getInt("st");
// 文字解析
JSONArray at = json.getJSONArray("at");
List<String> atList = new ArrayList<String>();
String up = at.getJSONObject(0).getString("a");
String down = at.getJSONObject(0).getString("b");
atList.add(at.getJSONObject(0).getString("a"));
atList.add(at.getJSONObject(0).getString("b"));
// 左、右、背景图片解析
JSONArray pic = json.getJSONArray("pic");
List<String> picList = new ArrayList<String>();
String left = pic.getJSONObject(0).getString("l");
String right = pic.getJSONObject(0).getString("r");
String backgroud = pic.getJSONObject(0).getString("b");
picList.add(left);
picList.add(right);
picList.add(backgroud);
String link = json.getString("link");
adBackInfo.setErr(err);
adBackInfo.setId(id);
adBackInfo.setNx(nx);
adBackInfo.setSt(st);
adBackInfo.setAt(atList);
adBackInfo.setPic(picList);
adBackInfo.setLink(link);
TextView upTextView = new TextView(context);
upTextView.setText(atList.get(0));
TextView downTextView = new TextView(context);
downTextView.setText(atList.get(0));
adBackViewData.setUpTextView(upTextView);
adBackViewData.setDownTextView(downTextView);
adBackViewData.getDownTextView().setText(atList.get(1));
adBackViewData.setlImageView(new ImageView(context));
adBackViewData.setrImageView(new ImageView(context));
adBackViewData.setBimageView(new ImageView(context));
List<ImageView> imageList = new ArrayList<ImageView>();
imageList.add(adBackViewData.getlImageView());
imageList.add(adBackViewData.getrImageView());
imageList.add(adBackViewData.getBimageView());
adBackViewData.getlImageView().setImageBitmap(DownLoadBitmap.getInstance().getBitmap(left));
adBackViewData.getrImageView().setImageBitmap(DownLoadBitmap.getInstance().getBitmap(right));
adBackViewData.getBimageView().setImageBitmap(DownLoadBitmap.getInstance().getBitmap(backgroud));
adBackViewData.getrImageView();
adBackViewData.getBimageView();
/*
* BitmapTask bitmapTask = new BitmapTask(picList, imageList); bitmapTask.execute();
*/
// new BitmapThread(urlList, imageList).start();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
/**
* 解析点击回传信息
*/
private static void parseClickInfo(JSONObject json)
{
}
/**
* 解析收集页面信息
*/
private static void parseInfoInfo(JSONObject json)
{
}
/**
* 解析sdk错误日志信息
*/
private static void parseLogsInfo(JSONObject json)
{
}
/**
* 根据流拿到字符串
* @param is
* @return
*/
private static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 1024);
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
// 过滤掉头信息,只返回JSON字符串
if (line.startsWith("{"))
{
sb.append(line);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}
}
package com.donson.host;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.content.Context;
import com.donson.common.ApplicationInfo;
import com.donson.util.LogInfo;
public class SendCommand
{
/**
* 上下文
*/
private Context context;
public SendCommand(Context context)
{
super();
this.context = context;
}
/**
* 没有JSON数据时调用
* @param method
*/
public void sendHttpCommand(Context context, String method)
{
sendHttpCommand(context, method, "");
}
/**
* 有JSON体时调用
* @param jsonString
* @param method
*/
public static void sendHttpCommand(Context context, String method, String jsonString)
{
String retSrc = "**";
// HttpPost request = new HttpPost(createUrl(method));
LogInfo.writeLog("sendCommand.sendHttpCommand", "-------------------------" + jsonString);
/* 创建http post连接 */
HttpPost httpRequest = new HttpPost(createUrl(method));
httpRequest.setParams(createHttpParams());
/*
* post运行,传递变量必须使用NameValuePair[]数组存储,通过HttpRequest.setEntity()方法来发送HTTP请求
*/
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("d", jsonString));
try
{
/* 发送HTTP request */
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/* 取得HTTP response */
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/* 若状态码为200 ok */
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
/* 取出答应字符串 */
String strResult = EntityUtils.toString(httpResponse.getEntity());
// InputStream is=(InputStream) httpResponse.getEntity();
ReturnInfoParser.parseReturnInfo(context, strResult, method);
// textview.setText(strResult);
retSrc = strResult;
}
else
{
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
public static final HttpParams createHttpParams()
{
final HttpParams params = new BasicHttpParams();
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, 15000);
HttpConnectionParams.setSoTimeout(params, 30000);
HttpConnectionParams.setSocketBufferSize(params, 8192 * 5);
return params;
}
/**
* 创建路径
* @param name
* @return
*/
private static String createUrl(String method)
{
ApplicationInfo appInfo = ApplicationInfo.getInstance();
// 将IP,端口号以及协议和方法名拼接成URL字符串
// http://open.donson.com/Api/init
String host = appInfo.baseSettingInfo.getHost();
StringBuffer url = new StringBuffer();
// 需要调用接口时的处理
url.append("http://").append(host).append("/Api").append("/").append(method);
return url.toString();
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("onCreate");
final String url = "https://martinreichart.com/_tmpdata/login_valid.json";
GetData(url);
}
public String GetData(String URL)
{
String returns="";
//Step One register scheme of https
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory (), 80));
//Step Two Get httpClient and httpPost
DefaultHttpClient client = new DefaultHttpClient();
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
//------- Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
// -------Example send http request
// final String url = "https://martinreichart.com/_tmpdata/login_valid.json";
HttpPost httpPost = new HttpPost(URL);
//Step Three Get Data
try {
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 解析返回的内容
HttpEntity entity = response.getEntity();
// returns = EntityUtils.toString(response.getEntity());
StringBuilder sb = new StringBuilder();
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
//读取
String line = null;
while ((line = reader.readLine())!= null){
sb.append(line + "\n");
}
reader.close();
System.out.println("解析完成");
// System.out.println(sb);
JSONObject array = new JSONObject(sb.toString());
System.out.println(array.length());
System.out.println(65);
}
}
}
catch(Exception ex)
{
System.out.println(86);
String aa =ex.toString();
ex.printStackTrace();
}
return returns;
}
}