我有以下字符串作为 HTTPResponse。它是 JSON 格式。
[
{
"From":"en",
"OriginalTextSentenceLengths":[
5
],
"TranslatedText":"Hallo",
"TranslatedTextSentenceLengths":[
5
]
},
{
"From":"en",
"OriginalTextSentenceLengths":[
8
],
"TranslatedText":"Frage",
"TranslatedTextSentenceLengths":[
5
]
},
{
"From":"en",
"OriginalTextSentenceLengths":[
6
],
"TranslatedText":"Antwort",
"TranslatedTextSentenceLengths":[
7
]
}
]
所以这个字符串我解析如下以获得“翻译文本数组”
String resp = "[{\"From\":\"en\",\"OriginalTextSentenceLengths\":[5],\"TranslatedText\":\"Hallo\",\"TranslatedTextSentenceLengths\":[5]},{\"From\":\"en\",\"OriginalTextSentenceLengths\":[8],\"TranslatedText\":\"Frage\",\"TranslatedTextSentenceLengths\":[5]},{\"From\":\"en\",\"OriginalTextSentenceLengths\":[6],\"TranslatedText\":\"Antwort\",\"TranslatedTextSentenceLengths\":[7]}]";
String[] stringArray = null;
try {
JSONArray finalResult=null;
JSONTokener tokener = new JSONTokener(resp);
finalResult = new JSONArray(tokener);
stringArray = new String[finalResult.length()];
for(int i=0;i<finalResult.length();i++){
JSONObject e = finalResult.getJSONObject(i);
Log.v("TAG",e.getString("TranslatedText"));
stringArray[i]=e.getString("TranslatedText");
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我正在从 JSON 对象中获取提取的“翻译文本”数组(Hallo、Frage、Antwort)......
但是,当我通过直接向 JSONTokener 提供与输入相同的字符串来执行相同的过程时,即在获得如下 HttpResponse 之后,我在 finalResult = new JSONArray(tokener) 行处得到 JSON 异常......
org.json.JSONException:在字符 0 处输入结束
String resp = getHttpResponse(uri);
String[] stringArray = null;
try {
JSONArray finalResult=null;
JSONTokener tokener = new JSONTokener(resp);
finalResult = new JSONArray(tokener);
stringArray = new String[finalResult.length()];
for(int i=0;i<finalResult.length();i++){
JSONObject e = finalResult.getJSONObject(i);
Log.v("TAG",e.getString("TranslatedText"));
stringArray[i]=e.getString("TranslatedText");
}
}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我已经努力了 2 天来解决此错误,但无法解决。所以我将其发布在这里...请帮助
编辑:
我正在添加 getHttpResponse 的实现
public static String getHttpResponse(URI uri) {
Log.d("APP_TAG", "Going to make a get request");
StringBuilder response = new StringBuilder();
try {
HttpGet get = new HttpGet();
get.setURI(uri);
//DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
Log.v("TAG","1");
HttpResponse httpResponse = httpClient.execute(get);
Log.v("TAG","2");
if (httpResponse.getStatusLine().getStatusCode() == 200) {
Log.d("demo", "HTTP Get succeeded");
HttpEntity messageEntity = httpResponse.getEntity();
InputStream is = messageEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
}
} catch (Exception e) {
Log.e("demo", e.getMessage());
}
Log.d("demo", "Done with HTTP getting");
return response.toString();
}
我给 getHttpResponse 的 uri 如下
String[] texts = {"hello","question","answer"};
final String params = "appId=" + URLEncoder.encode("78280AF4DFA1CE1676AFE86340C690023A5AC139","UTF-8")
+ "&from=" + URLEncoder.encode("en","UTF-8")
+ "&to=" + URLEncoder.encode("de","UTF-8")
+ "&texts=" + URLEncoder.encode(buildStringArrayParam(texts),"UTF-8");
final URL url = new URL("http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray?" + params);
URI myURI = java.net.URI.create(url.toString());
String resp = getHttpResponse(myURI);
这个响应字符串是我要解析的......
这是用于 buildStringArrayParam(texts)
StringBuilder targetString = new StringBuilder("[\"");
String value;
for(Object obj : values) {
if(obj!=null) {
value = obj.toString();
if(value.length()!=0) {
if(targetString.length()>2)
targetString.append(",\"");
targetString.append(value);
targetString.append("\"");
}
}
}
targetString.append("]");
return targetString.toString();