0

目前我无法找出问题所在。对于以下代码,我不断收到相同的错误消息:

package com.example.test1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class MainActivity extends Activity {
  private EditText text;
  private EditText text1;
  //private String translatedText;


  @Override
   public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Translate.setClientId("myId");
Translate.setClientSecret("mySecret");
    text = (EditText) findViewById(R.id.editText1);
    text1 = (EditText) findViewById(R.id.editText2);

    Button Trans1 = (Button)findViewById(R.id.button1);
    Trans1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View view) {
        String word = text.getText().toString();
        try {
            String translatedText = Translate.execute(word, Language.ENGLISH, Language.GERMAN);
            text1.setText(translatedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    });
    }
    }

错误:

02-14 16:36:26.355: W/System.err(1146): java.lang.Exception: [microsoft-translator-api] Error retrieving translation : null
02-14 16:36:26.361: W/System.err(1146):     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:202)
02-14 16:36:26.361: W/System.err(1146):     at com.memetix.mst.translate.Translate.execute(Translate.java:61)
02-14 16:36:26.361: W/System.err(1146):     at com.example.test1.MainActivity$1.onClick(MainActivity.java:35)
02-14 16:36:26.361: W/System.err(1146):     at android.view.View.performClick(View.java:4202)
02-14 16:36:26.361: W/System.err(1146):     at android.view.View$PerformClick.run(View.java:17340)
02-14 16:36:26.361: W/System.err(1146):     at android.os.Handler.handleCallback(Handler.java:725)
02-14 16:36:26.361: W/System.err(1146):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 16:36:26.361: W/System.err(1146):     at android.os.Looper.loop(Looper.java:137)
02-14 16:36:26.361: W/System.err(1146):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-14 16:36:26.372: W/System.err(1146):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 16:36:26.372: W/System.err(1146):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 16:36:26.372: W/System.err(1146):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-14 16:36:26.372: W/System.err(1146):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-14 16:36:26.372: W/System.err(1146):     at dalvik.system.NativeStart.main(Native Method)
02-14 16:36:26.372: W/System.err(1146): Caused by: android.os.NetworkOnMainThreadException
02-14 16:36:26.381: W/System.err(1146):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
02-14 16:36:26.381: W/System.err(1146):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
02-14 16:36:26.381: W/System.err(1146):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-14 16:36:26.381: W/System.err(1146):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-14 16:36:26.381: W/System.err(1146):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
02-14 16:36:26.381: W/System.err(1146):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
02-14 16:36:26.381: W/System.err(1146):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
02-14 16:36:26.391: W/System.err(1146):     at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:281)
02-14 16:36:26.401: W/System.err(1146):     at com.memetix.mst.MicrosoftTranslatorAPI.getToken(MicrosoftTranslatorAPI.java:133)
02-14 16:36:26.401: W/System.err(1146):     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:160)
02-14 16:36:26.401: W/System.err(1146):     at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)

或者我已经尝试过另一种方式:

package com.example.test1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;
import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class MainActivity extends Activity {
  private EditText text;
  private EditText text1;
  //private String translatedText;


  @Override
   public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Translate.setClientId("myId");
Translate.setClientSecret("mySecret");
    text = (EditText) findViewById(R.id.editText1);
    text1 = (EditText) findViewById(R.id.editText2);
    }

    public void onClick(View view) {
     switch (view.getId()) {
    case R.id.button1:
    if (text.getText().length() == 0) {
      Toast.makeText(this, "Please enter a word or phrase to translate...",
         Toast.LENGTH_LONG).show();
      return;
     }
    else {
    String word = text.getText().toString();
    try {
        String translatedText = Translate.execute(word, Language.ENGLISH, Language.GERMAN);
        text1.setText(translatedText);
    } catch (Exception e) {
        Toast.makeText(this, "flop...",
               Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
      }
     }
     }
     }

有了这个,它总是会捕获异常。

他们都没有返回结果。

主要问题(发现异常):

02-14 16:50:17.281: W/System.err(1249): java.lang.Exception: [microsoft-translator-api] Error retrieving translation : null
4

1 回答 1

5

引起:android.os.NetworkOnMainThreadException

这是因为您要么在不允许的 android 版本 >= 3.0 的主线程上执行网络操作。

使用 AsyncTask 从http://developer.android.com/reference/android/os/AsyncTask.html读取

于 2013-02-14T16:57:34.107 回答