提交 7bd7ff79 编写于 作者: Z ZhaoKaiQiang

添加对XML的支持

上级 4bc50272
......@@ -71,7 +71,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.github.zhaokaiqiang.klog/library/0.2.0/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" />
......@@ -84,9 +83,11 @@
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="library-0.2.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
<orderEntry type="library" exported="" name="httpclient-4.3.6" level="project" />
<orderEntry type="library" exported="" name="android-async-http-1.4.9" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
<orderEntry type="module" module-name="library" exported="" />
</component>
</module>
\ No newline at end of file
......@@ -24,11 +24,13 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.github.zhaokaiqiang.klog:library:0.2.0'
// compile project(':library')
compile 'com.loopj.android:android-async-http:1.4.9'
compile 'cz.msebera.android:httpclient:4.3.6'
// compile 'com.github.zhaokaiqiang.klog:library:0.2.0'
compile project(':library')
}
......@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.socks.sample">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".KLogApplication"
android:allowBackup="true"
......
......@@ -9,20 +9,31 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.socks.library.KLog;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity {
private static final String LOG_MSG = "KLog is a so cool Log Tool!";
private static String JSON;
private static final String TAG = "KLog";
private static final String URL = "http://www.w3school.com.cn/example/xmle/note.xml";
private static String XML = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><!-- Copyright w3school.com.cn --><note><to>George</to><from>John</from><heading>Reminder</heading><body>Don't forget the meeting!</body></note>";
private static String JSON;
private static String JSON_LONG;
private AsyncHttpClient httpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
httpClient = new AsyncHttpClient();
JSON_LONG = getResources().getString(R.string.json_long);
JSON = getResources().getString(R.string.json);
}
......@@ -81,6 +92,24 @@ public class MainActivity extends AppCompatActivity {
KLog.file(TAG, Environment.getExternalStorageDirectory(), "test.txt", JSON_LONG);
}
public void logWithXml(View view) {
KLog.xml(XML);
}
public void logWithXmlFromNet(View view) {
httpClient.get(this, URL, new TextHttpResponseHandler() {
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
KLog.d(responseString);
}
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
KLog.xml(responseString);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_about, menu);
......
......@@ -9,47 +9,59 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="log"
android:text="Log()" />
android:text="Log.d()" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithNull"
android:text="Log.d(NULL)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithMsg"
android:text="Log(String)" />
android:text="Log.d(String)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithTag"
android:text="Log(TAG,String)" />
android:text="Log.d(TAG,String)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithJson"
android:text="Log(JSON)" />
android:text="Log.json(JSON)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithLongJson"
android:text="Log(LONG JSON)" />
android:text="Log.json(LONG JSON)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithJsonTag"
android:text="Log(TAG,JSON)" />
android:text="Log.json(TAG,JSON)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithNull"
android:text="Log(NULL)" />
android:onClick="logWithFile"
android:text="Log.file(File)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithFile"
android:text="Log(File)" />
android:onClick="logWithXml"
android:text="Log.xml(XML)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="logWithXmlFromNet"
android:text="Log.xml(FROM NET)" />
</LinearLayout>
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
version = "0.2.0"
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
......@@ -20,11 +18,9 @@ android {
}
}
}
def siteUrl = 'https://github.com/ZhaoKaiQiang/KLog'
def gitUrl = 'https://github.com/ZhaoKaiQiang/KLog.git'
group = "com.github.zhaokaiqiang.klog"
install {
repositories.mavenInstaller {
pom {
......@@ -54,7 +50,6 @@ install {
}
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
......@@ -71,10 +66,8 @@ artifacts {
archives javadocJar
archives sourcesJar
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
......@@ -87,4 +80,7 @@ bintray {
licenses = ["Apache-2.0"]
publish = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
\ No newline at end of file
......@@ -65,7 +65,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/docs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
......@@ -75,12 +74,13 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/libs" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/poms" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="nekohtml" level="project" />
<orderEntry type="library" exported="" name="dom4j-2.0.0-RC1" level="project" />
<orderEntry type="library" exported="" name="xercesMinimal" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -121,6 +121,14 @@ public class KLog {
printLog(JSON, tag, jsonFormat);
}
public static void xml(String xml) {
printXml(null, xml);
}
public static void xml(String tag, String xml) {
printXml(tag, xml);
}
public static void file(File targetDirectory, Object msg) {
printFile(null, targetDirectory, null, msg);
}
......@@ -264,6 +272,43 @@ public class KLog {
}
}
private static void printXml(String tag, String html) {
if (!IS_SHOW_LOG) {
return;
}
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
int index = 4;
String className = stackTrace[index].getFileName();
String methodName = stackTrace[index].getMethodName();
int lineNumber = stackTrace[index].getLineNumber();
tag = (tag == null ? className : tag);
String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[ (").append(className).append(":").append(lineNumber).append(")#").append(methodNameShort).append(" ] ");
String headString = stringBuilder.toString();
if (html != null) {
html = XmlHelper.formatXML(html);
html = headString + "\n" + html;
}
printLine(tag, true);
String[] lines = html.split(LINE_SEPARATOR);
for (String line : lines) {
if (!isEmpty(line)) {
Log.d(tag, "║ " + line);
}
}
printLine(tag, false);
}
private static void printLine(String tag, boolean isTop) {
if (isTop) {
Log.d(tag, "╔═══════════════════════════════════════════════════════════════════════════════════════");
......@@ -272,4 +317,7 @@ public class KLog {
}
}
private static boolean isEmpty(String line) {
return TextUtils.isEmpty(line) || line.equals("\n") || line.equals("\r\n") || line.equals("\t");
}
}
\ No newline at end of file
package com.socks.library;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
/**
* Created by zhaokaiqiang on 15/11/18.
*/
public class XmlHelper {
public static String formatXML(String inputXML) {
XMLWriter writer = null;
String requestXML = null;
try {
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(inputXML));
StringWriter stringWriter = new StringWriter();
OutputFormat format = new OutputFormat(" ", true);
writer = new XMLWriter(stringWriter, format);
writer.write(document);
writer.flush();
requestXML = stringWriter.getBuffer().toString();
} catch (IOException e) {
e.printStackTrace();
return inputXML;
} catch (DocumentException e) {
e.printStackTrace();
return inputXML;
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
return inputXML;
}
}
}
return requestXML;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册