分类目录归档:andriod

andriod读取php的json时出现的bom问题

总是提示项目解析json数据时发现报
10-12 02:36:35.964: W/System.err(323): org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject
错误异常,无论更改header的编码还是用纯英文数据都依然提示,搜索一下错误提示,发现有人说是记事本(notepad.exe)保存的php文件保存时带有bom头,解决方案大概有下面几种:
1.继续用记事本保存成ansi就没问题了;
2.换个编辑器重新保存;
3.前面两种情况还是防不胜防,据说在andriod4.0里已有解决方案:
[java]
public JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
}
this.in = in;
}
[/java]
具体用法类似
[java]
String json = "{"
+ " \"query\": \"Pizza\", "
+ " \"locations\": [ 94043, 90210 ] "
+ "}";

JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
[/java]

Android读取word文件

Android中读取word文件需要用第三方jar包tm-extractors-0.4.jar。

[java]
package com.word.read;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.textmining.text.extraction.WordExtractor;

import Android.app.Activity;
import Android.os.Bundle;
import Android.os.Environment;
import Android.widget.TextView;

public class WordReader extends Activity {
/** Called when the activity is first created. */

private TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text = (TextView) findViewById(R.id.text);

String str = readWord(Environment.getExternalStorageDirectory().getAbsolutePath() + "/baojinggong.doc");
text.setText(str.trim().replace("\r", ""));
}

public String readWord(String file){
//创建输入流用来<a title="读取doc文件" href="http://www.android-study.com/pingtaikaifa/38.html">读取doc文件</a>
FileInputStream in;
String text = null;
try {
in = new FileInputStream(new File(file));
WordExtractor extractor = null;
//创建WordExtractor
extractor = new WordExtractor();
//进行提取对doc文件
text = extractor.extractText(in);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return text;
}
}
[/java]