我正在尝试从我使用Android应用程序制作的网站中提取内容。它是一个翻译应用程序,它根据放入的URL变量返回一个字符串。
但是,每当我收到网址时,我会得到com.example.reviantranslator.RetrieveFeedTask@b1eed738的变体,而不是我期望的翻译字符串。
我哪里错了?
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et = (EditText) findViewById(R.id.editText1);
final EditText et2 = (EditText) findViewById(R.id.editText2);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(getBaseContext(), "click", Toast.LENGTH_LONG).show();
String english = et.getText().toString();
String revian = et2.getText().toString();
if (!english.isEmpty() && revian.isEmpty()) {
String[] words = english.split(" ");
for (int i = 0; i < words.length; i++) {
try {
revian = revian + queryDB(words[i], false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
et2.setText(revian);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public String queryDB(String word, boolean revian) throws IOException {
String variab = "le";
if (revian) {
variab = "lr";
}
String fullurl = "http://maereti.com/p/Revian/revian.php?"+variab+"="+word;
RetrieveFeedTask RTF = new RetrieveFeedTask();
AsyncTask<String, String, String> out = RTF.execute(fullurl);
String output = out.toString();
return output;
}
public void ToastThis(String words) {
Toast.makeText(getBaseContext(), "toast function "+words, Toast.LENGTH_LONG).show();
}
}
和RetrieveFeedTask类:
public class RetrieveFeedTask extends AsyncTask<String, String, String> {
URL url;
@Override
protected String doInBackground(String... urls) {
String outputLine = "";
Looper.prepare();
MainActivity MA = new MainActivity();
try {
URL url = new URL(urls[0]);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
outputLine = outputLine + inputLine;
}
MA.ToastThis(outputLine);
in.close();
return outputLine;
} catch (Exception e) {
e.printStackTrace();
return "!! I am error !!";
}
}
protected void onPostExecute(String feed) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
答案 0 :(得分:1)
您没有从AsyncTask
检索结果,只是将任务本身转换为字符串。尝试更改
AsyncTask<String, String, String> out = RTF.execute(fullurl);
String output = out.toString();
到
AsyncTask<String, String, String> out = RTF.execute(fullurl);
String output = out.get();