Java Android异步任务不工作
doInBackgroung中的代码在java中工作得很好,但在android上就不行了。输出什么都不是,甚至表也不是。使用Android Studio。这里是android应用程序的完整代码 现在应用程序启动一个AsyncTask来连接到服务器,服务器只是PHP在单独使用java完成时会回显一些字符串HttpURLConnection会完美地捕捉到这些字符串。然而,在Android中,DownloadFilesTask的doInbackbround方法中的相同代码不会重现相同的效果 我试图捕获可能发生的任何异常,但没有抛出任何异常。对于任何真正了解Android开发的人来说,代码都是不言自明的 关于调试,函数showdata确实会被调用,但参数字符串似乎是空的。我在代码中添加了更多注释,以便更好地描述问题Java Android异步任务不工作,java,android,Java,Android,doInBackgroung中的代码在java中工作得很好,但在android上就不行了。输出什么都不是,甚至表也不是。使用Android Studio。这里是android应用程序的完整代码 现在应用程序启动一个AsyncTask来连接到服务器,服务器只是PHP在单独使用java完成时会回显一些字符串HttpURLConnection会完美地捕捉到这些字符串。然而,在Android中,DownloadFilesTask的doInbackbround方法中的相同代码不会重现相同的效果 我试图捕获
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MyActivity";
void showData(String s){ //gets called with an empty string
TextView t = (TextView) findViewById(R.id.textView);
t.setText(s);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_main);
// initAttendanceTable();
DownloadFilesTask task = new DownloadFilesTask();
task.execute();
initAttendanceTable();//just generates a tableRow and add it to a Table Row
// nothing fancy here but it actually is'nt doing anything after adding aynstask code
}
catch(Exception e) {
TextView t = (TextView) findViewById(R.id.textView);
t.setText(e.getLocalizedMessage()+"error");
}
}
private class DownloadFilesTask extends AsyncTask<Void,Void, String> {
protected String doInBackground(Void ...p) {
String results = new String("");
String temp = new String("");
try {
URL homeUrl = new URL("http://10.0.2.2:80/attendance/attendance_android.html");
//returns a few names ending with a null at the end
HttpURLConnection httpConn = (HttpURLConnection) homeUrl.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
if(br.ready()) {
results = br.readLine();
while(results!=null) {
results=br.readLine();
if(results!=null){
temp= temp + results;
}
}
}
}
catch(Exception e){
return null;
}
return temp;
}
//public ProgressDialog mProgressDialog;
protected void onProgressUpdate(Void ...progress) {
super.onProgressUpdate(progress);
//ProgressDialog.show(MainActivity.this, "loading", "wait...", true, true);
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result==null){ // to ctach error from doInbackground
Toast.makeText(getApplicationContext(), (String)result,
Toast.LENGTH_LONG).show();
}
showData(result);
}
}
private void initAttendanceTable() {
try{
TextView t=(TextView) findViewById(R.id.textView);
TableLayout attendanceTable = (TableLayout) findViewById(R.id.attendanceTable);
for (int j = 0; j < 3; j++) {
TableRow tbRow = new TableRow(this);
tbRow.setId(j);
tbRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
tbRow.setGravity(View.FOCUS_LEFT);
tbRow.setPadding(1, 1, 1, 1);
for (int i = 0; i < 3; i++) {
TextView tbRowText = new TextView(this);
tbRowText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.33f));
// tbRowText.setText(br.readLine());
tbRow.addView(tbRowText);
}
attendanceTable.addView(tbRow);
}
}
catch(Exception e){
TextView t=(TextView) findViewById(R.id.textView);
t.setText(e.getLocalizedMessage());
}
}
@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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
好的,那条线
if(br.ready())
是导致问题的原因删除此支票可以更正所有问题
if(br.ready()) {
results = br.readLine();
while(results!=null) {
results=br.readLine();
if(results!=null){
temp = temp + results;
}
}
}
将此更改为:
if(br.ready()) {
results = br.readLine();
while(results!=null) {
temp= temp + results;
results=br.readLine();
}
}
结果呢=null{results=br.readLine;..//这是您第二次执行readLine,第二次您可能会读取空字符串,因为您的服务器代码只回显了几个单词。请重试
task.executeOnExecutor(Executors.newScheduledThreadPool(1));
而不是
task.execute();
其他任务可能没有完全执行。您是否连接到服务器?catch语句中记录错误。catch中没有捕获错误,代码在java中运行良好。只是异步任务使事情变得复杂,我在清单文件中设置了权限,为什么会进行否决?这个问题是如何脱离主题的你能描述一下当代码在调试器中执行时你发现了什么吗?执行与你的期望有何偏差?删除一堆代码,只给出一个非常模糊的问题描述,对每个人来说都是浪费时间。在代码中添加跟踪语句并跟踪其执行。当你说它不工作时king,如果没有额外的细节,这并不意味着很多。showData运行了吗?传递给它的值是什么?onPostExecute接收到的是空字符串吗?你得到的是否决票,因为这个问题很清楚解决它所需的细节……顺便说一句,我注意到你正在执行initAttendanceTable;两次,一次,在启动AsyncTa之前sk,然后立即再次…此外,您应该为temp使用StringBuilder,并将循环减少为whileresults=br.readLine!=null{temp.appendresults;}