gpt4 book ai didi

java - 如何使用按钮搜索将搜索查询传递给另一个 Activity

转载 作者:行者123 更新时间:2023-12-02 02:08:07 25 4
gpt4 key购买 nike

我正在开发一个用于求职的 Android 应用程序,这里使用 PHP 作为后端。从服务器获取并显示为卡片 View 的工作列表运行良好。这里我想搜索位置和工作头衔。那么如何将我的位置和职位名称从搜索 Activity 传递到职位 Activity 。

 private void searchjob() {

final String title = jobtitle.getText().toString().trim();
final String location = city.getSelectedItem().toString().trim();

Intent intent = new Intent(HomeActivity.this, MainActivity.class);
intent.putExtra(title,1);
intent.putExtra(location,2);


startActivity(intent);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
}

这是我的下载卡 java 类。

public class DownloaderCard extends AsyncTask<Void, Integer, String> {
Context c;
String address;
SwipeDeck swipeDeck;
ProgressDialog pd;
String url = "";

public DownloaderCard(Context c, String address, SwipeDeck swipeDeck) {
this.c = c;
this.address = address;
this.swipeDeck = swipeDeck;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Please Wait");
pd.setMessage("Loading....");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
String data = downloadData();
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
if(s!=null){
//Toast.makeText(c, s, Toast.LENGTH_LONG).show();
ParserCard p = new ParserCard(c, s, swipeDeck);
p.execute();
}else {
Toast.makeText(c, "Unable to download data from downloader", Toast.LENGTH_LONG).show();
}
}
private String downloadData(){
InputStream is = null;
String line = null;
try {
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
if(br != null){
while ((line = br.readLine()) != null){
sb.append(line+"\n");
}

}else {
return null;
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null){
try {
is.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
return null;
}
}

这是我的 JobActivity。

String urlData = "https://www.joblist.php";

final DownloaderCard dl = new DownloaderCard(MainActivity.this,urlData , cardStack);
dl.execute();
cardStack.setEventCallback(new SwipeDeck.SwipeEventCallback() {
@Override
public void cardSwipedLeft(int position) {
// Log.i("MainActivity", "card was swiped left, position in adapter: " + position);
}

@Override
public void cardSwipedRight(int position) {
// Log.i("MainActivity", "card was swiped right, position in adapter: " + position);
}

@Override
public void cardsDepleted() {
// Log.i("MainActivity", "no more cards");
}

@Override
public void cardActionDown() {
// Log.i(TAG, "cardActionDown");
}

@Override
public void cardActionUp() {
// Log.i, "cardActionUp");
}

});
cardStack.setLeftImage(R.id.left_image);
cardStack.setRightImage(R.id.right_image);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cardStack.swipeTopCardLeft(180);

}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cardStack.swipeTopCardRight(180);
}
});
}

最佳答案

这是我用来将登录信息发送到 PHP 服务器的方法。更新它,使其适合您的目的。

      public String LoginOnService(String url,String email,String pass){
InputStream inputStream = null;
String result = "";
try {

// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();

// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url+"?email="+email+"&password="+pass);
// 3. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
if(httpResponse != null){
result = EntityUtils.toString(httpResponse.getEntity());
}else{
result = "Did not work!";
}
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}

// 11. return result
return result;
}

正如您所看到的,我正在传递一个 String,但如果您想以 JSON 形式发送数据,您也可能会从这些注释中受益。但在步骤2中只传递一个url HttpPost httpPost = new HttpPost(url);

 public String LoginOnService(String url,String email,String pass){
InputStream inputStream = null;
String result = "";
try {

// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();

// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url+"?email="+email+"&password="+pass);

// String json = YOUR_JSON_STRING;

// 3. build jsonObject
// JSONObject jsonObject = new JSONObject();
// jsonObject.accumulate("Email", user.getEmail());
// jsonObject.accumulate("Password", user.getPassword());

// 4. convert JSONObject to JSON to String
// json = jsonObject.toString();

// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);

// 5. set json to StringEntity
// StringEntity se = new StringEntity(json);

// 6. set httpPost Entity
// httpPost.setEntity(se);

// 7. Set some headers to inform server about the type of the content
// httpPost.setHeader("Accept", "application/json");
// httpPost.setHeader("Content-type", "application/json");

// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);

// 9. receive response as inputStream
// inputStream = httpResponse.getEntity().getContent();

// 10. convert inputstream to string
if(httpResponse != null)
result = EntityUtils.toString(httpResponse.getEntity());
else
result = "Did not work!";

} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}

// 11. return result
return result;
}

关于java - 如何使用按钮搜索将搜索查询传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com