I am back from taking a few years break in programming. Today I am trying to access my webserver from android and I have some code I recycled from back in the day. The code used to work, but, lo and behold, today it has an error. Can someone help me figure this out?
Here is my main class:
public class login extends AppCompatActivity {
Button join;
TextView clientid;
EditText username, password;
_upload upload;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
upload = new _upload();
String android_id = Secure.getString(login.this.getContentResolver(),
Secure.ANDROID_ID);
join = findViewById(R.id.join);
clientid = findViewById(R.id.clientid);
clientid.setText(android_id);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
join.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}});
}
public void login(){
String id = username.getText().toString();
if (id.isEmpty()) { username.setError("required");username.requestFocus();return; }
String pw = password.getText().toString();
String cid = clientid.getText().toString();
String[] params = new String[3];
params[1]="username::" id;
params[2]="password::" pw;
params[3]="cid::" cid;
new upload.send(login.this, "dump.php", params);
Toast.makeText(this, id " " pw " " cid, Toast.LENGTH_LONG).show();
}
}
my error is in the line new upload.send(login.this, "dump.php", params);
error: cannot find symbol
new _upload.send(login.this, "dump.php", params);
^
symbol: class send
location: class _upload
this is my second class, the one that used to work:
public class _upload extends AppCompatActivity {
HttpURLConnection conn = null;
String Return;
String homeurl = "removed";
String roomurl = "";
String param;
Context ctx;
String er;
public void location(Context context, String url, String params){
ctx = context;
roomurl = url;
try {
param = "lola=" URLEncoder.encode(params, "UTF-8");
new sendStatusChange_Server().execute("");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void send(Context context, String url, String params[]){
ctx = context;
roomurl = url;
int total = params.length;
int i = 0;
while(i<=total-1) {
if (i==0) {
try {
String[] keyval = params[0].split("::");
param = keyval[0] "=" URLEncoder.encode(keyval[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i ;
}
else{
try {
String[] keyval = params[i].split("::");
param = param "&" keyval[0] "=" URLEncoder.encode(keyval[1], "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i ;
}
}
new sendStatusChange_Server().execute("");
}
public class sendStatusChange_Server extends AsyncTask<String, String, Void> {
protected Void doInBackground(String... params) {
try {
updateserver();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(er!=null){Toast.makeText(ctx, er, Toast.LENGTH_LONG).show();}
else{Toast.makeText(ctx, Return, Toast.LENGTH_LONG).show();}
}
}
private void updateserver() throws IOException {
URL url = new URL(homeurl roomurl);
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
Log.d("SENT:", param " to " url.toString());
out.close();
String response = "";
Scanner inStream = new Scanner(conn.getInputStream());
while (inStream.hasNextLine())
response = (inStream.nextLine());
inStream.close();
Return = response;
} catch (MalformedURLException ex) {
} catch (IOException ex) {
er = ex.toString();
}
return;
}
}
the code still runs fine on the old program but I made a new package and want to get that rolling... why would this happen? Thank you for taking the time!
CodePudding user response:
You have a syntax error. Use
upload.send(...)
instead of
new upload.send(...)
since upload
is already an instance of your class.
You should probably also make it so _upload
doesn't extend AppCompatActivity
(just remove the extends AppCompatActivity
from public class _upload extends AppCompatActivity
).