1
我正在創建一個Android應用程序,該應用程序可以使用HTTP DELETE從JSON中刪除數據。但我面臨的問題。我創建了2個不同的文件。 MainActivity和Async之一。無法在我的Async類中找到方法
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
onClickButtonListner();
}
public void onClickButtonListner() {
final Button btn = (Button) findViewById(R.id.delete_btn);
final String URL = myurl;
btnDelete.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Async asyn = new Async(URL);
asyn.execute();
}
});
}
}
Async.java
public class Async extends AsyncTask<Void, Void, String> {
String murl;
public Async(String url) {
murl = url;
}
@Override
protected String doInBackground(Void... params) {
HttpURLConnection httpURLConnection;
URL url ;
try {
final EditText id= (EditText)findViewById(R.id.delete_id);
**showing unable to resolve method findViewById**
String delete_url = murl + "/" + id.getText().toString();
url = new URL(delete_url);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestMethod("DELETE");
httpURLConnection.connect();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
所以我的問題是,我的異步類是顯示無法找到findViewById方法。
所以,請幫助我如何解決這個問題 謝謝
謝謝@pRaNaY現在我也想通過使用單個AsyncTask來實現GET,POST,PUT。目前,我對每個GET,POST,PUT都有不同的AsyncTask。所以告訴我如何在它之間切換。意思是當我按Update時update方法在異步任務中會運行等 – Ghost
你能幫我嗎 – Ghost
你可以在Asynk的構造函數中添加新的參數來實現其他方法。例如:public Async(String url,String _id,String methodType){..}'並在'doInBackground'中使用方法類型。 – pRaNaY