我正在尝试设置我的应用程序与服务器通信,以便我可以执行配置文件CRUD。我设置了一个FAB,即单击时将创建一个Jonobject并将数据发布到服务器上,所有代码似乎都很好,但是在" 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
之后添加Parens,因为您正在尝试调用构造函数。换句话说,用new SendUserDetails.execute(...)
替换CC_2。