gpt4 book ai didi

android - AsyncTask中的错误

转载 作者:行者123 更新时间:2023-12-03 08:34:05 26 4
gpt4 key购买 nike

嗨,我是Android编程的新手,被建议使用AsyncTask进行网络连接。在将它们添加到doInBackground之前,我收到了有关有效区域的错误:

(ex cannot be resolved)- in the catch section



和onPostExecute:

Syntax error on token ")", ; expected- for (String Result)

(The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (LongOperation, String, int))- for Toast.makeText



这些错误与我放置代码的位置有关吗?我真的很欢迎任何适合我的代码的答案。
package com.example.clearlight;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URL;

import org.apache.http.client.ResponseHandler;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.StrictMode;
import android.util.Log;

public class MainActivity extends Activity {

TextView txt;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);


setContentView(R.layout.relative);

class LongOperation extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

URL url = null;

DefaultHttpClient httpclient = null;

try {

String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
url = new URL(registrationUrl);

HttpGet getRequest = new HttpGet(registrationUrl);
ResponseHandler<String> handler = new BasicResponseHandler();
httpclient = new DefaultHttpClient();
// request data from server
String result = httpclient.execute(getRequest, handler);
Log.d("MyApp", "Data from server is "+ result);}

catch (Exception ex) {Log.e("error",ex.toString());
ex.printStackTrace();

}


@Override
protected void onPostExecute(String result)
{
TextView text1 = (TextView) findViewById(R.id.text);

//Sets the new text to TextView (runtime click event)//*******
text1.setText("Light Data= " + result);

Toast.makeText(this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
//txtMessage.setText(String.valueOf(msg1) + " " + String.valueOf(msg2));
}


}
}

}
}

最佳答案

这些只是语法错误,与AsyncTask无关。

格式化代码(Ctrl-Shift-F)可使解决此问题变得更加容易!

doInBackground方法之后,您缺少大括号,需要将return null从头到尾移动。

    @Override
protected String doInBackground(String... params) {
URL url = null;

DefaultHttpClient httpclient = null;

try {

String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
url = new URL(registrationUrl);

HttpGet getRequest = new HttpGet(registrationUrl);
ResponseHandler<String> handler = new BasicResponseHandler();
httpclient = new DefaultHttpClient();
// request data from server
String result = httpclient.execute(getRequest, handler);
Log.d("MyApp", "Data from server is " + result);
} catch (Exception ex) {
Log.e("error", ex.toString());
ex.printStackTrace();

}

return null;
} // THIS HERE

@Override
protected void onPostExecute(String result) {
TextView text1 = (TextView) findViewById(R.id.text);

//Sets the new text to TextView (runtime click event)//*******
text1.setText("Light Data= " + result);

Toast.makeText(this, "Light Data:" + result, Toast.LENGTH_SHORT).show(); //MESSAGE BOX
//txtMessage.setText(String.valueOf(msg1) + " " + String.valueOf(msg2));
}

我查看了您的代码,并且我认为以下内容可能会起作用(尽管我只是在StackOverflow上执行了此操作,但它可能会为您提供更多提示您出问题的地方:
        @Override
protected String doInBackground(String... params) {
String result = "Failed";
try {
String registrationUrl = "http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
// request data from server
URL url = new URL(registrationUrl);
HttpGet getRequest = new HttpGet(registrationUrl);
DefaultHttpClient httpclient = new DefaultHttpClient();

HttpResponse response = httpclient.execute(getRequest);
result = convertStreamToString(response.getEntity().getContent());
Log.d("MyApp", "Data from server is " + result);
} catch (Exception ex) {
Log.e("MyApp", "Failed to load light data", ex);
}

return result;
} // THIS HERE

@Override
protected void onPostExecute(String result) {
TextView text1 = (TextView) findViewById(R.id.text);
text1.setText("Light Data= " + result);

Toast.makeText(MainActivity.this, "Light Data:" + result, Toast.LENGTH_SHORT).show();
}

public String convertStreamToString(InputStream inputStream) throws IOException {
if (inputStream != null) {
Writer writer = new StringWriter();

char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024);
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
inputStream.close();
}
return writer.toString();
} else {
return "";
}
}

关于android - AsyncTask中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15463200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com