Issue
This is my code file
It will results true for all searched strings.I don't know why?
this is my JSON file on the server
https://vikasbajpayee.000webhostapp.com/villagesapi.php
Php code file
link of php code
Please tell me if anybody knows here .thanks in advance.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(name!= null && !TextUtils.isEmpty(name)&&!name.equals("null"))
new AsyncFetch(name).execute();
else
Toast.makeText(Checkout.this, "Please Enter Village name", Toast.LENGTH_SHORT).show();
}
});
private class AsyncFetch extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(Checkout.this);
HttpURLConnection conn;
URL url = null;
String searchQuery;
public AsyncFetch(String searchQuery){
this.searchQuery=searchQuery;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
url = new URL("https://vikasbajpayee.000webhostapp.com/villagesapi.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
// setDoInput and setDoOutput to true as we send and recieve data
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder().appendQueryParameter("searchQuery", searchQuery);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return("Connection error");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
pdLoading.dismiss();
if (result.equals("no rows")) {
Toast.makeText(Checkout.this, "No Village exist with this name", Toast.LENGTH_LONG).show();
} else {
//success
Toast.makeText(Checkout.this, "Success", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(Checkout.this,FinalCheckout.class);
intent.putExtra("namedata",name);
startActivity(intent);
}
}
}
}
I am searching for a "name" after button click in MySQL PHP database how can I do this.so that result would be searched the name on another activity .this code is not working because it is getting true for all the searched strings(name here).it runs in all cases.
Solution
Android side:
You are not actually searching for anything. You just fetch the data from a specific URL.Which gives you same data with every request.
you have to add some parameter to URL like this:
example.com/villagesapi.php?name=myvillage
Server side:
And in your PHP script, you have to generate response according to the parameter that requested to it. you can do it by like this:
if(isset($_GET['name'])) {
//search from database where name = $_GET['name']
//then echo json_encode(database response array)
}
and current problem is here,
if (result.equals("no rows")) {
Toast.makeText(Checkout.this, "No Village exist with this name", Toast.LENGTH_LONG).show();
}
You are checking if the response is "no rows" then show a false message. But you always get a JSON string response which is not "no rows". That's why for all request, you get true.
Note: In Android, you should not search for a string with .contain() method.You should parse the JSON string for the name.
Answered By - Md. Rezve Hasan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.