【问题标题】:android class async upload referenceandroid类异步上传参考
【发布时间】:2022-01-20 00:37:42
【问题描述】:

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

这是我的主要课程:

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

这是我的第二节课,曾经工作过的那节:

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

}

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

【问题讨论】:

  • 你不应该永远像那样手动创建活动 (new _upload())。而且你的代码和错误信息不一致,缺少_
  • 我以前从未遇到过问题(创建后台任务),下划线是复制粘贴错误,我仍然有同样的问题。你有什么建议吗?
  • 据我所知,您有_upload 扩展AppCompatActivity - 这是为什么呢?在此处显示的代码中,您并没有将其用作实际的 Activity,如果您尝试在其中调用 Activity 基类方法,则会导致各种潜在问题。至于您的包裹错误,这里没有足够的信息可以帮助您。
  • 你的意思是打电话给upload.send(...)而不是new _upload.send(...)吗?没有理由在那里创建一个新实例,而且后者的语法也不正确。
  • 非常感谢,我只需要放弃“新”!

标签: java android class reference


【解决方案1】:

您有语法错误。使用

upload.send(...)

而不是

new upload.send(...)

因为upload 已经是你的类的一个实例。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2018-12-06
    相关资源
    最近更新 更多