使用dx的各种悲剧错误,一直一直搞啊搞,搞了三天之后才把这么个小错误搞定
做程序员不易浮躁啊,还有就是要真的是要锲而不舍啊!!!!!!!
错误一:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: abc.class: file not found
at com.android.dx.util.FileUtils.readFile(FileUtils.java:55)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.j
ava:134)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java
:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
1 error; aborting
当你出现这个运行时错误的话,那就是你需要编译的文件需要和你安卓的platform-tool这个文件夹中的内容放在一起
你要做的就是把你的文件放在和dexdump.exe文件和dx.bat放在同一个目录下
这个样子就可以编译通过成功生成dex文件了
错误二:
trouble processing: bad class file magic (cafebabe) or version (0033.0000) ...while parsing Hello.class ...while processing Hello.class 1 warning no classfiles specified
出现这个错误的原因是因为jdk的版本不对,我之前用的是JDK1.7但是不知道为什么他编译之后生成的JDK的版本会低于jdk1.6真是让我百思不得其解?
也不知道这个代码是怎么写的
之后我上网查了一下,我把JDK1.7换成了JDK1.6,可是还是不行,出现了相同的提示,结果展示如下
这个时候解决的办法有一个:
强制转换成1.6版本的class
使用的到的命令就是javac -source 1.6 -target 1.6 ****.java(****代表了你的java文件名称)
具体步骤演示如下:
1.编写一个java文件
2.使用javac命令:javac -source 1.6 -target 1.6 ******.java 注意你编写的必须是在你的android的platform-tools文件目录下
3.使用dx命令 dx --dex --output =*****.dex *****.class
dexdump.exe -d *****.dex
这个时候我们来看一下结果
D:\Android\android-sdk\platform-tools>dx --dex --output=abc.dex abc.class
D:\Android\android-sdk\platform-tools>dexdump.exe -d abc.dex
Processing 'abc.dex'...
Opened 'abc.dex', DEX version '035'
Class #0 -
Class descriptor : 'Labc;'
Access flags : 0x0001 (PUBLIC)
Superclass : 'Ljava/lang/Object;'
Interfaces -
Static fields -
Instance fields -
Direct methods -
#0 : (in Labc;)
name : '<init>'
type : '()V'
access : 0x10001 (PUBLIC CONSTRUCTOR)
code -
registers : 1
ins : 1
outs : 1
insns size : 4 16-bit code units
000160: |[000160] abc.<init>:()V
000170: 7010 0500 0000 |0000: invoke-direct {v0}, Ljava/
lang/Object;.<init>:()V // method@0005
000176: 0e00 |0003: return-void
catches : (none)
positions :
0x0000 line=2
locals :
0x0000 - 0x0004 reg=0 this Labc;
#1 : (in Labc;)
name : 'main'
type : '([Ljava/lang/String;)V'
access : 0x0009 (PUBLIC STATIC)
code -
registers : 3
ins : 1
outs : 2
insns size : 30 16-bit code units
000178: |[000178] abc.main:([Ljava/lang/S
tring;)V
000188: 1250 |0000: const/4 v0, #int 5 // #5
00018a: 1271 |0001: const/4 v1, #int 7 // #7
00018c: 2311 0600 |0002: new-array v1, v1, [I // ty
pe@0006
000190: 2601 0800 0000 |0004: fill-array-data v1, 000000
0c // +00000008
000196: 7120 0200 1000 |0007: invoke-static {v0, v1}, La
bc;.test:(I[I)V // method@0002
00019c: 0e00 |000a: return-void
00019e: 0000 |000b: nop // spacer
0001a0: 0003 0400 0700 0000 0100 0000 0100 ... |000c: array-data (18 units)
catches : (none)
positions :
0x0000 line=18
0x000a line=19
0x000b line=18
locals :
#2 : (in Labc;)
name : 'test'
type : '(I[I)V'
access : 0x0089 (PUBLIC STATIC VARARGS)
code -
registers : 6
ins : 2
outs : 2
insns size : 20 16-bit code units
0001c4: |[0001c4] abc.test:(I[I)V
0001d4: 2151 |0000: array-length v1, v5
0001d6: 1200 |0001: const/4 v0, #int 0 // #0
0001d8: 3510 0c00 |0002: if-ge v0, v1, 000e // +000
c
0001dc: 4402 0500 |0004: aget v2, v5, v0
0001e0: 6203 0000 |0006: sget-object v3, Ljava/lang
/System;.out:Ljava/io/PrintStream; // field@0000
0001e4: 6e20 0300 2300 |0008: invoke-virtual {v3, v2}, L
java/io/PrintStream;.print:(I)V // method@0003
0001ea: d800 0001 |000b: add-int/lit8 v0, v0, #int
1 // #01
0001ee: 28f5 |000d: goto 0002 // -000b
0001f0: 6200 0000 |000e: sget-object v0, Ljava/lang
/System;.out:Ljava/io/PrintStream; // field@0000
0001f4: 6e20 0400 4000 |0010: invoke-virtual {v0, v4}, L
java/io/PrintStream;.println:(I)V // method@0004
0001fa: 0e00 |0013: return-void
catches : (none)
positions :
0x0000 line=10
0x0006 line=12
0x000b line=10
0x000e line=14
0x0013 line=15
locals :
Virtual methods -
source_file_idx : 12 (abc.java)
如果你有任何的问题,可以和我联系:243148194@qq.com
之前走了好多弯路希望能帮上大家!^^
在工作中要用到android,然后进行网络请求的时候,打算使用httpClient。
总结一下httpClient的一些基本使用。
版本是4.2.2。
使用这个版本的过程中,百度很多,结果都是出现的org.apache.commons.httpclient.这个包名,而不是我这里的org.apache.http.client.HttpClient----------前者版本是Commons HttpClient 3.x ,不是最新的版本HttpClient 4.×。
官网上面:
Commons HttpClient 3.x codeline is at the end of life. All users of Commons HttpClient 3.x are strongly encouraged to upgrade to HttpClient 4.1.
1.基本的get
public void getUrl(/blog_article/String url, String encoding/index.html)
throws ClientProtocolException, IOException {
// 默认的client类。
HttpClient client = new DefaultHttpClient();
// 设置为get取连接的方式.
HttpGet get = new HttpGet(url);
// 得到返回的response.
HttpResponse response = client.execute(get);
// 得到返回的client里面的实体对象信息.
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("内容编码是:" + entity.getContentEncoding());
System.out.println("内容类型是:" + entity.getContentType());
// 得到返回的主体内容.
InputStream instream = entity.getContent();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream, encoding));
System.out.println(reader.readLine());
} catch (Exception e) {
e.printStackTrace();
} finally {
instream.close();
}
}
// 关闭连接.
client.getConnectionManager().shutdown();
}
2.基本的Post
下面的params参数,是在表单里面提交的参数。
public void postUrlWithParams(String url, Map params, String encoding)
throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
// 添加参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null && params.keySet().size() > 0) {
Iterator iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Entry) iterator.next();
nvps.add(new BasicNameValuePair((String) entry.getKey(),
(String) entry.getValue()));
}
}
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine()
+ entity.getContent());
dump(entity, encoding);
System.out.println("Post logon cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
} finally {
// 关闭请求
httpclient.getConnectionManager().shutdown();
}
}
3。打印页面输出的小代码片段
private static void dump(HttpEntity entity, String encoding)
throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
entity.getContent(), encoding));
System.out.println(br.readLine());
}
4.常见的登录session问题,需求:使用账户,密码登录系统之后,然后再访问页面不出错。
特别注意,下面的httpclient对象要使用一个,而不要在第二次访问的时候,重新new一个。至于如何保存这个第一步经过了验证的httpclient,有很多种方法实现。单例,系统全局变量(android 下面的Application),ThreadLocal变量等等。
以及下面创建的httpClient要使用ThreadSafeClientConnManager对象!
public String getSessionId(String url, Map params, String encoding,
String url2) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient(
new ThreadSafeClientConnManager());
try {
HttpPost httpost = new HttpPost(url);
// 添加参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if (params != null && params.keySet().size() > 0) {
Iterator iterator = params.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Entry) iterator.next();
nvps.add(new BasicNameValuePair((String) entry.getKey(),
(String) entry.getValue()));
}
}
// 设置请求的编码格式
httpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
// 登录一遍
httpclient.execute(httpost);
// 然后再第二次请求普通的url即可。
httpost = new HttpPost(url2);
BasicResponseHandler responseHandler = new BasicResponseHandler();
System.out.println(httpclient.execute(httpost, responseHandler));
} finally {
// 关闭请求
httpclient.getConnectionManager().shutdown();
}
return "";
}
5.下载文件,例如mp3等等。
//第一个参数,网络连接;第二个参数,保存到本地文件的地址
public void getFile(String url, String fileName) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
public byte[] handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toByteArray(entity);
} else {
return null;
}
}
};
byte[] charts = httpClient.execute(get, handler);
FileOutputStream out = new FileOutputStream(fileName);
out.write(charts);
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
6.创建一个多线程环境下面可用的httpClient
(原文:http://blog.csdn.net/jiaoshi0531/article/details/6459468)
HttpParams params = new BasicHttpParams();
//设置允许链接的做多链接数目
ConnManagerParams.setMaxTotalConnections(params, 200);
//设置超时时间.
ConnManagerParams.setTimeout(params, 10000);
//设置每个路由的最多链接数量是20
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20);
//设置到指定主机的路由的最多数量是50
HttpHost localhost = new HttpHost("127.0.0.1",80);
connPerRoute.setMaxForRoute(new HttpRoute(localhost), 50);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
//设置链接使用的版本
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
//设置链接使用的内容的编码
HttpProtocolParams.setContentCharset(params,
HTTP.DEFAULT_CONTENT_CHARSET);
//是否希望可以继续使用.
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80));
schemeRegistry.register(new Scheme("https",SSLSocketFactory.getSocketFactory(),443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params,schemeRegistry);
httpClient = new DefaultHttpClient(cm, params);
7.实用的一个对象,http上下文,可以从这个对象里面取到一次请求相关的信息,例如request,response,代理主机等。
public static void getUrl(/blog_article/String url, String encoding/index.html)
throws ClientProtocolException, IOException {
// 设置为get取连接的方式.
HttpGet get = new HttpGet(url);
HttpContext localContext = new BasicHttpContext();
// 得到返回的response.第二个参数,是上下文,很好的一个参数!
httpclient.execute(get, localContext);
// 从上下文中得到HttpConnection对象
HttpConnection con = (HttpConnection) localContext
.getAttribute(ExecutionContext.HTTP_CONNECTION);
System.out.println("socket超时时间:" + con.getSocketTimeout());
// 从上下文中得到HttpHost对象
HttpHost target = (HttpHost) localContext
.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
System.out.println("最终请求的目标:" + target.getHostName() + ":"
+ target.getPort());
// 从上下文中得到代理相关信息.
HttpHost proxy = (HttpHost) localContext
.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
if (proxy != null)
System.out.println("代理主机的目标:" + proxy.getHostName() + ":"
+ proxy.getPort());
System.out.println("是否发送完毕:"
+ localContext.getAttribute(ExecutionContext.HTTP_REQ_SENT));
// 从上下文中得到HttpRequest对象
HttpRequest request = (HttpRequest) localContext
.getAttribute(ExecutionContext.HTTP_REQUEST);
System.out.println("请求的版本:" + request.getProtocolVersion());
Header[] headers = request.getAllHeaders();
System.out.println("请求的头信息: ");
for (Header h : headers) {
System.out.println(h.getName() + "--" + h.getValue());
}
System.out.println("请求的链接:" + request.getRequestLine().getUri());
// 从上下文中得到HttpResponse对象
HttpResponse response = (HttpResponse) localContext
.getAttribute(ExecutionContext.HTTP_RESPONSE);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("返回结果内容编码是:" + entity.getContentEncoding());
System.out.println("返回结果内容类型是:" + entity.getContentType());
dump(entity, encoding);
}
}
输出结果大致如下:
socket超时时间:0 最终请求的目标:money.finance.sina.com.cn:-1 是否发送完毕:true 请求的版本:HTTP/1.1 请求的头信息: Host--money.finance.sina.com.cn Connection--Keep-Alive User-Agent--Apache-HttpClient/4.2.2 (java 1.5) 请求的链接:/corp/go.php/vFD_BalanceSheet/stockid/600031/ctrl/part/displaytype/4.phtml 返回结果内容编码是:null 返回结果内容类型是:Content-Type: text/html
8.设置代理
//String hostIp代理主机ip,int port 代理端口 HttpHost proxy = new HttpHost(hostIp, port); // 设置代理主机. httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
9.设置保持链接时间
//在服务端设置一个保持持久连接的特性.
//HTTP服务器配置了会取消在一定时间内没有活动的链接,以节省系统的持久性链接资源.
httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response,
HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch (Exception e) {
}
}
}
HttpHost target = (HttpHost)context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
if("www.baidu.com".equalsIgnoreCase(target.getHostName())){
return 5*1000;
}
else
return 30*1000;
}
});
转账注明出处:http://renjie120.iteye.com/blog/1727933
static用法小结
static关键字是C, C++中都存在的关键字, 它主要有三种使用方式, 其中前两种只指在C语言中使用, 第三种在C++中使用(C,C++中具体细微操作不尽相同, 本文以C++为准).
(1)局部静态变量
(2)外部静态变量/函数
(3)静态数据成员/成员函数
下面就这三种使用方式及注意事项分别说明
一、局部静态变量
在C/C++中, 局部变量按照存储形式可分为三种auto, static, register
(<C语言程序设计(第二版)>谭浩强, 第174-175页)
与auto类型(普通)局部变量相比, static局部变量有三点不同
1. 存储空间分配不同
auto类型分配在栈上, 属于动态存储类别, 占动态存储区空间, 函数调用结束后自动释放, 而static分配在静态存储区, 在程序整个运行期间都不释放. 两者之间的作用域相同, 但生存期不同.
2. static局部变量在所处模块在初次运行时进行初始化工作, 且只操作一次
3. 对于局部静态变量, 如果不赋初值, 编译期会自动赋初值0或空字符, 而auto类型的初值是不确定的. (对于C++中的class对象例外, class的对象实例如果不初始化, 则会自动调用默认构造函数, 不管是否是static类型)
特点: static局部变量的”记忆性”与生存期的”全局性”
所谓”记忆性”是指在两次函数调用时, 在第二次调用进入时, 能保持第一次调用退出时的值.
示例程序一
#include <iostream>
using namespace std;
void staticLocalVar()
{
static int a = 0; // 运行期时初始化一次, 下次再调用时, 不进行初始化工作
cout<<"a="<<a<<endl;
++a;
}
int main()
{
staticLocalVar(); // 第一次调用, 输出a=0
staticLocalVar(); // 第二次调用, 记忆了第一次退出时的值, 输出a=1
return 0;
}
应用:
利用”记忆性”, 记录函数调用的次数(示例程序一)
利用生存期的”全局性”, 改善”return a pointer / reference to a local object”的问题. Local object的问题在于退出函数, 生存期即结束,. 利用static的作用, 延长变量的生存期.
示例程序二:
// IP address to string format
// Used in Ethernet Frame and IP Header analysis
const char * IpToStr(UINT32 IpAddr)
{
static char strBuff[16]; // static局部变量, 用于返回地址有效
const unsigned char *pChIP = (const unsigned char *)&IpAddr;
sprintf(strBuff, "%u.%u.%u.%u", pChIP[0], pChIP[1], pChIP[2], pChIP[3]);
return strBuff;
}
注意事项:
1. “记忆性”, 程序运行很重要的一点就是可重复性, 而static变量的”记忆性”破坏了这种可重复性, 造成不同时刻至运行的结果可能不同.
2. “生存期”全局性和唯一性. 普通的local变量的存储空间分配在stack上, 因此每次调用函数时, 分配的空间都可能不一样, 而static具有全局唯一性的特点, 每次调用时, 都指向同一块内存, 这就造成一个很重要的问题 ---- 不可重入性!!!
这样在多线程程序设计或递归程序设计中, 要特别注意这个问题.
(不可重入性的例子可以参见<effective C++ (2nd)>(影印版)第103-105页)
下面针对示例程序二, 分析在多线程情况下的不安全性.(为方便描述, 标上行号)
① const char * IpToStr(UINT32 IpAddr)
② {
③ static char strBuff[16]; // static局部变量, 用于返回地址有效
④ const unsigned char *pChIP = (const unsigned char *)&IpAddr;
⑤ sprintf(strBuff, "%u.%u.%u.%u", pChIP[0], pChIP[1], pChIP[2], pChIP[3]);
⑥ return strBuff;
⑦ }
假设现在有两个线程A,B运行期间都需要调用IpToStr()函数, 将32位的IP地址转换成点分10进制的字符串形式. 现A先获得执行机会, 执行IpToStr(), 传入的参数是0x0B090A0A, 顺序执行完应该返回的指针存储区内容是:”10.10.9.11”, 现执行到⑥时, 失去执行权, 调度到B线程执行, B线程传入的参数是0xA8A8A8C0, 执行至⑦, 静态存储区的内容是192.168.168.168. 当再调度到A执行时, 从⑥继续执行, 由于strBuff的全局唯一性, 内容已经被B线程冲掉,
此时返回的将是192.168.168.168字符串, 不再是10.10.9.11字符串.
二、外部静态变量/函数
在C中static有了第二种含义:用来表示不能被其它文件访问的全局变量和函数。, 但为了限制全局变量/函数的作用域, 函数或变量前加static使得函数成为静态函数。但此处“static”的含义不是指存储方式,而是指对函数的作用域仅局限于本文件(所以又称内部函数)。注意此时, 对于外部(全局)变量, 不论是否有static限制, 它的存储区域都是在静态存储区, 生存期都是全局的. 此时的static只是起作用域限制作用, 限定作用域在本模块(文件)内部.
使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名。
示例程序三:
//file1.cpp
static int varA;
int varB;
extern void funA()
{
……
}
static void funB()
{
……
}
//file2.cpp
extern int varB; // 使用file1.cpp中定义的全局变量
extern int varA; // 错误! varA是static类型, 无法在其他文件中使用
extern vod funA(); // 使用file1.cpp中定义的函数
extern void funB(); // 错误! 无法使用file1.cpp文件中static函数
三、静态数据成员/成员函数(C++特有)
C++重用了这个关键字,并赋予它与前面不同的第三种含义:表示属于一个类而不是属于此类的任何特定对象的变量和函数. 这是与普通成员函数的最大区别, 也是其应用所在, 比如在对某一个类的对象进行计数时, 计数生成多少个类的实例, 就可以用到静态数据成员. 在这里面, static既不是限定作用域的, 也不是扩展生存期的作用, 而是指示变量/函数在此类中的唯一性. 这也是”属于一个类而不是属于此类的任何特定对象的变量和函数”的含义. 因为它是对整个类来说是唯一的, 因此不可能属于某一个实例对象的. (针对静态数据成员而言,
成员函数不管是否是static, 在内存中只有一个副本, 普通成员函数调用时, 需要传入this指针, static成员函数调用时, 没有this指针. )
请看示例程序四(<effective c++ (2nd)>(影印版)第59页)
class EnemyTarget {
public:
EnemyTarget() { ++numTargets; }
EnemyTarget(const EnemyTarget&) { ++numTargets; }
~EnemyTarget() { --numTargets; }
static size_t numberOfTargets() { return numTargets; }
bool destroy(); // returns success of attempt to destroy EnemyTarget object
private:
static size_t numTargets; // object counter
};
// class statics must be defined outside the class;
// initialization is to 0 by default
size_t EnemyTarget::numTargets;
在这个例子中, 静态数据成员numTargets就是用来计数产生的对象个数的.
另外, 在设计类的多线程操作时, 由于POSIX库下的线程函数pthread_create()要求是全局的, 普通成员函数无法直接做为线程函数, 可以考虑用Static成员函数做线程函数.