无法解析符号执行,android
Cannot resolve symbol execute, android
我正在尝试设置我的应用程序以与服务器通信,以便我可以执行配置文件增删改查。我建立了一个 fab,当点击时会创建一个 JSONObject 和 post 那个数据到服务器,所有的代码似乎都很好,但是在 "new SendUserDetails.execute("server_URL",postData.toString());"说它无法解析符号执行。有什么我忽略的吗?提前感谢您的帮助。
edit_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JSONObject postData = new JSONObject();
try{
postData.put("Name", name.getText().toString());
postData.put("Bio", bio.getText().toString());
postData.put("Age", age.getText().toString());
postData.put("Major", major.getText().toString());
postData.put("College", college.getText().toString());
postData.put("Random Fact", random_fact.getText().toString());
new SendUserDetails.execute("server_URL", postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
这是 Http 连接的代码
private class SendUserDetails extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection httpURLConnection = null;
try{
httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData" + params[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while(inputStreamData != -1){
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(httpURLConnection != null){
httpURLConnection.disconnect();
}
}
return data;
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
Log.e("TAG", result);
}
}
在 SendUserDetails
之后添加括号,因为您正在尝试调用构造函数。换句话说,将 new SendUserDetails.execute(...)
替换为 new SendUserDetails().execute(...)
.
我正在尝试设置我的应用程序以与服务器通信,以便我可以执行配置文件增删改查。我建立了一个 fab,当点击时会创建一个 JSONObject 和 post 那个数据到服务器,所有的代码似乎都很好,但是在 "new SendUserDetails.execute("server_URL",postData.toString());"说它无法解析符号执行。有什么我忽略的吗?提前感谢您的帮助。
edit_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
JSONObject postData = new JSONObject();
try{
postData.put("Name", name.getText().toString());
postData.put("Bio", bio.getText().toString());
postData.put("Age", age.getText().toString());
postData.put("Major", major.getText().toString());
postData.put("College", college.getText().toString());
postData.put("Random Fact", random_fact.getText().toString());
new SendUserDetails.execute("server_URL", postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
这是 Http 连接的代码
private class SendUserDetails extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection httpURLConnection = null;
try{
httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData" + params[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while(inputStreamData != -1){
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(httpURLConnection != null){
httpURLConnection.disconnect();
}
}
return data;
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
Log.e("TAG", result);
}
}
在 SendUserDetails
之后添加括号,因为您正在尝试调用构造函数。换句话说,将 new SendUserDetails.execute(...)
替换为 new SendUserDetails().execute(...)
.