我是Android编程的新手,我需要你的帮助 我正在创建一个购物清单应用程序,并在我的ServerConnect.java文件中提供了许多错误,请求您建议我更正代码,错误来自错误标识符预期或illigal开始的类型 提前感谢您的帮助
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.HashMap;
public abstract class ServerConnect {
private GetFromServer getFromServer;
public ServerConnect(Context context,HashMap<String,String> payloads){
getFromServer = new GetFromServer(context,getUrlEncoder(payloads));
}
public void execute(String url){
getFromServer.execute(url);
}
private String getUrlEncoder(HashMap<String,String> payloads){
Uri.Builder builder = new Uri.Builder();
for (HashMap.Entry<String,String> temp: payloads.entrySet()){
Log.w("HM", temp.getKey() + "," + temp.getValue());
builder.appendQueryParameter(temp.getKey(),temp.getValue());
}
return builder.build().getEncodedQuery();
}
public abstract void getResult(String result);
private class GetFromServer extends AsyncTask<String,Void,String>{
private ProgressDialog progressDialog;
private String urlEncodedPayLoads;
public GetFromServer(Context context,String urlEncodedPayLoads){
progressDialog = ProgressDialog.show(context,"Wait","Connecting Server");
this.urlEncodedPayLoads = urlEncodedPayLoads;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String… params) {
StringBuilder stringBuilder = new StringBuilder();
progressDialog.setMessage("Connected, Please wait");
try {
URL url = new URL(params[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTf-8"));
bufferedWriter.write(urlEncodedPayLoads);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
String line = "";
while ((line = bufferedReader.readLine()) != null){
stringBuilder.append(line).append("\n");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
getResult(s);
}
}
}
请参阅下面的logcat错误
请在下面找到错误日志
Error:(65, 47) error: illegal character: '\u2026'
Error:(65, 48) error: ';'expected
Error:(65, 55) error: illegal start of type
Error:(65, 56) error: <identifier> expected
Error:(65, 58) error: ';' expected
Error:(66, 40) error: <identifier> expected
Error:(67, 38) error: <identifier> expected
Error:(67, 39) error: illegal start of type
Error:(68, 13) error: illegal start of type
Error:(68, 16) error: ';' expected
Error:(71, 51) error: <identifier> expected
Error:(71, 52) error: illegal start of type
Error:(72, 52) error: <identifier> expected
Error:(72, 53) error: illegal start of type
Error:(73, 49) error: <identifier> expected
Error:(73, 50) error: illegal start of type
Error:(74, 53) error: <identifier> expected
Error:(74, 54) error: illegal start of type
Error:(74, 70) error: illegal start of type
Error:(75, 45) error: <identifier> expected
Error:(75, 46) error: illegal start of type
Error:(76, 46) error: <identifier> expected
Error:(76, 47) error: illegal start of type
Error:(81, 37) error: <identifier> expected
Error:(81, 56) error: <identifier> expected
Error:(82, 37) error: <identifier> expected
Error:(83, 37) error: <identifier> expected
Error:(84, 35) error: <identifier> expected
Error:(86, 42) error: <identifier> expected
Error:(94, 17) error: illegal start of type
Error:(94, 24) error: illegal start of type
Error:(94, 25) error: ')' expected
Error:(94, 29) error: ';' expected
Error:(94, 55) error: <identifier> expected
Error:(94, 57) error: ';' expected
Error:(98, 15) error: illegal start of type
Error:(100, 15) error: illegal start of type
Error:(102, 15) error: illegal start of type
Error:(106, 13) error: illegal start of type
Error:(106, 19) error: ';' expected
Error:(106, 33) error: illegal start of type
Error:(111, 19) error: class, interface, or enum expected
Error:(113, 13) error: class, interface, or enum expected
Error:(114, 13) error: class, interface, or enum expected
Error:(115, 9) error: class, interface, or enum expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
答案 0 :(得分:2)
您必须从某处复制/粘贴此代码。
protected String doInBackground(String… params)
无效,特别是String
之后的省略号字符。这应该是三个点,例如:
protected String doInBackground(String... params)
很难想象破碎代码的所有红色亮点都没有给你足够的帮助。