令牌上的语法错误,错位的构造***安卓错误***

时间:2012-04-06 02:59:12

标签: android

我是Android开发的新手,我正在制作一个使用Google翻译API翻译内容的程序。错误如下:'令牌上的语法错误,错位的构造。谢谢。这是我的Java文件:

package org.example.translate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import java.io.BufferedReader;

public class TranslateTask implements Runnable {
    private static final String TAG = "TranslateTask";
    private final Translate translate;
    private final String original, from, to;

    TranslateTask(Translate translate, String original, String from, String to) {
        this.translate = translate;
        this.original = original;
        this.from = from;
        this.to = to;
    }

    public void run() {
        String trans = doTranslate(original, from, to);
        translate.setTranslated(trans);

        String retrans = doTranslate(trans, from, to);
        translate.setRetranslated(retrans);
    }

    private String doTranslate (String original, String from, String to) {
        String result = translate.getResources().getString(
                    R.string.translation_error);
        HttpURLConnection con = null;
        Log.d(TAG, "dotranslate(" + original + ", " + from + ", " + to + ")");

//*This is where the error occours*// try {
            if (Thread.interrupted())
                throw new InterruptedException();

                // Build RESTful query for Google API
                String q = URLEncoder.encode(original, "utf-8");
                URL url = new URL(
                        "http://ajax.googleapis.com/ajax/services/language/translate" 
                        + "?v=1.0" + "&q=" + q + "&langpair=" + from
                        + "%7C" + to);
            con = (HttpURLConnection) url.openConnection();
            con.setReadTimeout(10000);
            con.setConnectTimeout(15000);
            con.setRequestMethod("GET");
            con.addRequestProperty("referer", 
                "http://www.pragprog.com/titles/eband3/hello-android");
            con.setDoInput(true);

            con.connect();

        if (Thread.interrupted())
            throw new InterruptedException();

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(con.getInputStream(), "utf-8"));    
            String payload = reader.readLine();
            reader.close();

            JSONObject jsonObject = new JSONObject(payload);
            result = jsonObject.getJSONObject("responseData")
                .getString("translatedText")
                .replace("'", "'")
                .replace("&", "&");

        if (Thread.interrupted())finally
            throw new InterruptedException();

        } catch ("IOException", e) {
            Log.e(TAG, "IOException" e);
        }
        catch ("JSONException", e) {
            Log.e(TAG, "JSONException", e);
        }
        catch (InterruptedEception e) {
            Log.d(TAG, "InterruptedException", e);
            result = translate.getResources().getString(
                    R.string.translation_interrupted);
        }
        finally {
            if (con!= null) {
                con.disconnect();
        }   
    };
};

}

请帮帮我!感谢明确的答案

1 个答案:

答案 0 :(得分:0)

我能看到的唯一一件事(可能是复制/粘贴错误)就是你在其中一行上有一个随机的finally

        JSONObject jsonObject = new JSONObject(payload);
        result = jsonObject.getJSONObject("responseData")
            .getString("translatedText")
            .replace("'", "'")
            .replace("&", "&");

    if (Thread.interrupted())finally  // <-- this doesn't belong :)
        throw new InterruptedException();

    } catch ("IOException", e) {
        Log.e(TAG, "IOException" e);
    }