2015-04-03 166 views
0

我一直在嘗試創建一個翻譯器應用程序,並且我已經設置了相應的所有內容。但是,我仍然得到這個特定的錯誤,並試圖解決它一段時間。Microsoft Translator錯誤:檢索翻譯錯誤

package mobi.the404.appointed; 

import android.content.Intent; 
import android.database.Cursor; 
import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.memetix.mst.language.Language; 
import com.memetix.mst.translate.Translate; 

import java.util.ArrayList; 
import java.util.List; 

public class TranslateActivity extends ActionBarActivity{ 
    private TextView textViewReceived; 
    private TextView textViewTranslatedText; 
    private Spinner spinner; 
    private Button buttonTTranslate; 
    private Button buttonTBack; 
    private String selectedAp; 
    private DBAdapter db = new DBAdapter(this); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_translate); 
     Translate.setClientId("ID"); 
     Translate.setClientSecret("SECRET"); 

     this.setTitle("Translate Appointment"); 
     Intent intent = getIntent(); 
     Bundle extras = intent.getExtras(); 
     if(extras != null) { 
      selectedAp = intent.getExtras().getString("selectedAp"); 
     } 

     textViewReceived = (TextView) findViewById(R.id.textViewRecieved); 
     textViewTranslatedText = (TextView) findViewById(R.id.textViewTranslatedText); 
     spinner = (Spinner) findViewById(R.id.spinner); 
     buttonTBack = (Button) findViewById(R.id.buttonTBack); 
     buttonTTranslate = (Button) findViewById(R.id.buttonTTranslate); 
     try { 
      loadDetails(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     buttonTBack.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(TranslateActivity.this, AppointedActivity.class); 
       startActivity(intent); 
       try { 
        this.finalize(); 
       } catch (Throwable throwable) { 
        throwable.printStackTrace(); 
       } 
      } 
     }); 

     buttonTTranslate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        textViewTranslatedText.setText(translate(textViewReceived.getText().toString(), spinner.getSelectedItem().toString())); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

    } 

    private void loadDetails() throws Exception { 
     db.open(); 
     Cursor item = db.getRecord(selectedAp); 
     textViewReceived.append(item.getString(3)); 
     db.close(); 


     List<String> listLangs = new ArrayList<>(); 

     listLangs.add("Arabic"); 
     listLangs.add("Dutch"); 
     listLangs.add("German"); 
     listLangs.add("French"); 
     listLangs.add("Greek"); 
     listLangs.add("Hindi"); 
     listLangs.add("Italian"); 
     listLangs.add("Portuguese"); 
     listLangs.add("Thai"); 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listLangs); 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner.setAdapter(dataAdapter); 
    } 

    public String translate(String text, String lang) throws Exception { 
     String translatedText = ""; 

     Language send = null; 
     if(lang == "Arabic"){ 
      send = Language.ARABIC; 
     } if(lang == "Dutch"){ 
      send = Language.DUTCH; 
     } if(lang == "German"){ 
      send = Language.GERMAN; 
     } if(lang == "French"){ 
      send = Language.FRENCH; 
     } if(lang == "Greek"){ 
      send = Language.GREEK; 
     } if(lang == "Hindi"){ 
      send = Language.HINDI; 
     } if(lang == "Italian"){ 
      send = Language.ITALIAN; 
     } if(lang == "Portuguese"){ 
      send = Language.PORTUGUESE; 
     } if(lang == "Thai"){ 
      send = Language.THAI; 
     } 
     Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show(); 
     translatedText = Translate.execute(text, send); 
     return translatedText; 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_translate, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

而給出的錯誤是這個;

04-03 14:51:41.721 23897-23897/mobi.the404.appointed W/System.err﹕ java.lang.Exception: [microsoft-translator-api] Error retrieving translation : null 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.translate.Translate.execute(Translate.java:61) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.translate.Translate.execute(Translate.java:76) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at mobi.the404.appointed.TranslateActivity.translate(TranslateActivity.java:129) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at mobi.the404.appointed.TranslateActivity$2.onClick(TranslateActivity.java:73) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.view.View.performClick(View.java:5184) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.view.View$PerformClick.run(View.java:20910) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.Looper.loop(Looper.java:145) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5942) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 
04-03 14:51:41.726 23897-23897/mobi.the404.appointed W/System.err﹕ Caused by: android.os.NetworkOnMainThreadException 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:215) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:367) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:295) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:208) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199) 
04-03 14:51:41.736 23897-23897/mobi.the404.appointed W/System.err﹕ ... 14 more 
04-03 14:52:45.296 23897-23897/mobi.the404.appointed V/ActivityThread﹕ updateVisibility : ActivityRecord{2c2786f6 [email protected] {mobi.the404.appointed/mobi.the404.appointed.TranslateActivity}} show : true 

應用程序不會崩潰,但會給出錯誤。我究竟做錯了什麼?

PS:我已經在清單中爲互聯網訪問提供了許可。

感謝you.ID

回答

0

這條線在日誌中是最重要的一條:

Caused by: android.os.NetworkOnMainThreadException

所以每次使用函數調用Translate.execute時間,創建一個新的主題:

new Thread(){ 
    public void run() { 

     // ... 
     translate(...); 
     // ... 

    } 
}.start(); 
+0

這不是一個完整的解決方案,因爲你忽略了'translate'方法的返回值。如果你得到它,你應該更新UI,這不能從後臺線程完成,但可以使用'runOnUiThread'來實現。 – nikis 2015-04-03 10:10:59

+0

是的,你的答案更完整。我的回答只是爲了提示*指向正確的方向,我不想重寫他的代碼。無論如何,我upvoted你的答案:) – ByteHamster 2015-04-03 11:14:47

2

這裏的問題是從主線程禁止網絡調用API 11,否則主線程(UI線程)將被阻塞wai爲響應。如果你把你會發現堆棧跟蹤,一看就是當你調用下面一行在你的點擊處理程序:

textViewTranslatedText.setText(translate(textViewReceived.getText().toString(), spinner.getSelectedItem().toString())); 

網絡呼叫將由API您使用在以下行來進行:

Translate.execute(text, send); 

要避免此異常,您應該以異步方式調用Translate.execute。最簡單的方法是使用AsyncTask如果這個網絡的要求並不需要太多的時間:

private static class MyAsyncTask extends AsyncTask<Request, Void, String>{ 

    private WeakReference<TextView> translatedView; 

    public MyAsyncTask(TextView translatedView) { 
     this.translatedView = new WeakReference<TextView>(translatedView); 
    } 

    @Override 
    protected String doInBackground(Request... params) { 
     Request req = params[0]; 
     String translatedText = Translate.execute(req.getText(), req.getLanguage()); 
     return translatedText; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     TextView output = translatedView.get(); 
     if (output != null){ 
      output.setText(s); 
     } 
    } 
} 

private static class Request { 
    private String text; 
    private Language language; 

    public Request(Language language, String text) { 
     this.language = language; 
     this.text = text; 
    } 

    public Language getLanguage() { 
     return language; 
    } 

    public void setLanguage(Language language) { 
     this.language = language; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 
} 

不過,也有關於如何做異步操作,就像使用IntentService另一個方法,創建HandlerThread,等解決方案取決於你的需求和要求。你可以在這裏閱讀一些信息http://blog.nikitaog.me/2014/10/11/android-looper-handler-handlerthread-i/

+0

謝謝大家! :) – 2015-04-03 10:14:41

+0

仍給出了相同的錯誤! :/ – 2015-04-03 11:02:41

+0

顯示你做了什麼。你以前使用過'AsyncTask'嗎? – nikis 2015-04-03 11:05:08