android class 异步上传参考

android class async upload reference

我在编程领域休息了几年回来了。今天,我正尝试从 android 访问我的网络服务器,并且我有一些我以前回收的代码。代码曾经有效,但是,你瞧,今天它有一个错误。有人可以帮我解决这个问题吗?

这是我的主要内容 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();


}

}

我的错误在行 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

这是我的第二个 class,曾经有效的那个:

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;
}

}

代码在旧程序上仍然可以正常运行,但我制作了一个新程序包并希望继续运行...为什么会发生这种情况?感谢您抽出宝贵时间!

您有语法错误。使用

upload.send(...)

而不是

new upload.send(...)

因为 upload 已经是您的 class 的一个实例。

您可能还应该这样做 _upload 不会扩展 AppCompatActivity(只需从 public class _upload extends AppCompatActivity 中删除 extends AppCompatActivity)。