您好我是Android编程的新手,并建议使用AsyncTask建立我的网络连接。在将它们添加到doInBackground之前,我收到了有关工作区域的错误:
(ex无法解决) - 在catch部分
和onPostExecute:
令牌上的语法错误“)”,; expected- for(String Result)
(Toast类型中的方法makeText(Context,CharSequence,int)是 不适用于参数(LongOperation,String,int)) - 对于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));
}
}
}
}
}
答案 0 :(得分:1)
删除
return null;
在启动doInBackground
方法并关闭此方法时插入右括号并删除代码的最后一个大括号。
@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();
}
}
答案 1 :(得分:0)
这些只是语法错误,与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 "";
}
}